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

Mandy Chung mandy.chung at oracle.com
Mon Nov 2 19:53:05 UTC 2015

> On Oct 20, 2015, at 11:28 AM, Roger Riggs <roger.riggs at oracle.com> wrote:
> Updated Javadoc:
>  http://cr.openjdk.java.net/~rriggs/cleaner-doc/

I’m happy with this API to provide an easy way to migrate away from finalizers.  Some thoughts:
1. For an existing finalizer (say cleanup function), it will be replaced with
     Cleaner.create().phantomCleanable(this, this::cleanup);

It may be useful to provide a sharable Cleaner instance that doesn’t go away.

The primary use for this Cleaner API is to replace finalizers.  I wonder it worths having a convenient method easier to relate to finalizer (say Cleaner.finalizable?) 

2. Do you know any existing use of soft / weak references in the JDK can benefit from this automatic cleanup mechanism? The typical cache implementation using soft/weak references is to purge the entries when the cache is accessed and manages the reference queue without a background thread.

I’m in two minds that it sounds sensible for this Cleaner API to extend for soft/weak references whereas I am not certain of the use cases.   Anyone can share the known use cases that would be helpful.

> Updated Webrev:
>   http://cr.openjdk.java.net/~rriggs/webrev-cleaner-8138696/

  57     final PhantomCleanable<?> phantomCleanableList;
  59     final WeakCleanable<?> weakCleanableList;
  61     final SoftCleanable<?> softCleanableList;

- is there any benefit of keeping separate list for each reference type?  Each cleaner has one single ReferenceRueue and it seems that keeping one single Cleanable list could achieve similar performance in most cases while the implementation might be simplified.

 111         thread.setPriority(Thread.MAX_PRIORITY);

VM threads are near max priority.  The reference handler thread is MAX_PRORITY whereas the finalizer thread is MAX_PRIORITY-2.   I think the cleaner thread should not be of MAX_PRIORITY and also perhaps the caller may need a way to get the thread associated with a Cleaner so that users can set the priority if needed?


More information about the core-libs-dev mailing list