RFR 9: 8138696 : java.lang.ref.Cleaner - an easy to use alternative to finalization

Eamonn McManus eamonn at mcmanus.net
Thu Oct 1 15:11:39 UTC 2015


I think this is a great idea. In addition to the NetBeans precedent
cited in the Issue, I'll note that Guava has
FinalizableReferenceQueue. Like the NetBeans solution, that has proved
tricky to make collectable in a context where its ClassLoader might
become otherwise unreferenced. Having the facility in the JRE obviates
that problem.

As a minor note, the javadoc says "phantomCleanup registers the
Runnable to be run when object is not phantom reachable", which I
don't think is technically accurate. The java.lang.ref definition of
reachability is that an object becomes (for example) phantom reachable
when it is referenced by a phantom reference but no longer has any
stronger level of reachability. So the text here should say "when the
object becomes phantom reachable". Likewise the per-method text "when
the object is unreachable by a PhantomReference" should say "becomes
phantom reachable". And of course the same, mutatis mutandis, for the
soft and weak equivalents.
Éamonn


2015-10-01 7:12 GMT-07:00 Roger Riggs <Roger.Riggs at oracle.com>:
> Please review a proposal for public Cleaner API:
>
> A Cleaner is proposed to provide an easy to use alternative to finalization.
> The service would provide easy registration and cancellation of cleanup
> functions for objects. Applications create a cleanup service for their own
> use and the service terminates when it is no longer in use.
>
> Finalization has a long history of issues both in usage and performance.
> PhantomReferences have been proposed as the alternative GC based mechanism
> for cleaning functions but it has been left as an exercise to the developer
> to construct the necessary mechanisms to handle ReferenceQueues, handle
> threading issues and robust termination.
>
> The Cleaner performs cleaning functions when objects are unreachable as
> found by garbage collection using the existing mechanisms of
> PhantomReference, WeakReference, SoftReferences, and ReferenceQueues. It
> manages a thread that dequeues references to unreachable objects and invokes
> the corresponding cleaning function. Registered cleaning functions can be
> cleared if no longer needed, can be invoked explicitly to perform the
> cleanup immediately, or be invoked when the object is not reachable (as
> detected by garbage collection) and handled by a cleanup thread.
>
> The java.lang.ref package is proposed for the Cleaner because it is
> complementary to the reference classes and reference queues and to make it
> easy to find.
>
> It is not a goal to replace all uses of finalization or sun.misc.Cleaner in
> the JDK.
> Investigation will evaluate if and in what cases the Cleaner can replace
> finalization.
> A subsequent task will examine uses of finalization and propose specific
> changes
> on a case by base basis.
>
> Please review and comment:
>
> Javadoc:
>   http://cr.openjdk.java.net/~rriggs/cleaner-doc/
>
> Webrev:
>    http://cr.openjdk.java.net/~rriggs/webrev-cleaner-8138696/
>
> Issue:
>    https://bugs.openjdk.java.net/browse/JDK-8138696
>
> Thanks, Roger
>



More information about the core-libs-dev mailing list