Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8138696

java.lang.ref.Cleaner - an alternative to finalization

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b99

      Description

      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.

      It is not a goal to replace all uses of finalization or sun.misc.Cleaner. Investigation will evaluate if and in what cases the cleaner can replace finalization.

      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.

        Issue Links

          Activity

          Hide
          flar Jim Graham added a comment -
          Other similar mechanisms currently in use:

          sun.java2d.Disposer
          com.sun.prism.impl.Disposer
          com.sun.javafx.font,Disposer
          java.com.sun.webkit.Disposer.java
          Show
          flar Jim Graham added a comment - Other similar mechanisms currently in use: sun.java2d.Disposer com.sun.prism.impl.Disposer com.sun.javafx.font,Disposer java.com.sun.webkit.Disposer.java
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/7c8488605485
          User: rriggs
          Date: 2015-12-21 16:45:40 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/7c8488605485 User: rriggs Date: 2015-12-21 16:45:40 +0000
          Hide
          chegar Chris Hegarty added a comment -
          [~rriggs] now that this API is in the JDK, can we create a single Cleaner for the base module ( or possibly the platform itself )? Id like to move the nio usages of sun.misc.Cleaner and on to the new API.
          Show
          chegar Chris Hegarty added a comment - [~rriggs] now that this API is in the JDK, can we create a single Cleaner for the base module ( or possibly the platform itself )? Id like to move the nio usages of sun.misc.Cleaner and on to the new API.
          Hide
          rriggs Roger Riggs added a comment -
          I planned to add a common cleaner with the first batch of finalizer changes but the common cleaner can be added sooner.
          What I had in mind is in this prototype webrev:
           http://cr.openjdk.java.net/~rriggs/webrev-cleaning-finalizers/src/java.base/share/classes/jdk/internal/misc/CleanerFactory.java.html

          If you are ready for that, we can do it sooner.
          Show
          rriggs Roger Riggs added a comment - I planned to add a common cleaner with the first batch of finalizer changes but the common cleaner can be added sooner. What I had in mind is in this prototype webrev:   http://cr.openjdk.java.net/~rriggs/webrev-cleaning-finalizers/src/java.base/share/classes/jdk/internal/misc/CleanerFactory.java.html If you are ready for that, we can do it sooner.
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/7c8488605485
          User: lana
          Date: 2015-12-23 23:04:19 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/7c8488605485 User: lana Date: 2015-12-23 23:04:19 +0000

            People

            • Assignee:
              rriggs Roger Riggs
              Reporter:
              rriggs Roger Riggs
            • Votes:
              1 Vote for this issue
              Watchers:
              11 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: