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

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



    • Enhancement
    • Resolution: Fixed
    • P3
    • 9
    • 9
    • core-libs
    • b99


      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



              rriggs Roger Riggs
              rriggs Roger Riggs
              1 Vote for this issue
              11 Start watching this issue