Uploaded image for project: 'CCC Migration Project'
  1. CCC Migration Project
  2. CCC-8071507

(ref) Clear phantom references when enqueued, like soft and weak references

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 9
    • core-libs
    • None
    • minimal
    • Hide
      This change has minimal compatibility risk.

      1) `PhantomReference::get` always returns null to ensure that the referent
         of a phantom reference cannot be retrieved.

      2) An object becomes phantom reachable after it has been finalized. Although
         this change may cause the phantom-reachable objects to be GC'ed earlier --
         previously the referent is kept alive until PhantomReference objects
         are GC'ed or cleared by the application -- existing code would only depend
         on the PhantomReference being enqueued rather than when the referent is
         freed from the heap.
      Show
      This change has minimal compatibility risk. 1) `PhantomReference::get` always returns null to ensure that the referent    of a phantom reference cannot be retrieved. 2) An object becomes phantom reachable after it has been finalized. Although    this change may cause the phantom-reachable objects to be GC'ed earlier --    previously the referent is kept alive until PhantomReference objects    are GC'ed or cleared by the application -- existing code would only depend    on the PhantomReference being enqueued rather than when the referent is    freed from the heap.
    • Java API
    • SE

      Summary

      Make phantom references be automatically cleared by the garbage collector, like soft and weak references.

      Problem

      Phantom references are not automatically cleared by the garbage collector when they are enqueued, unlike soft and weak references. When an object R (the "referent") becomes phantom reachable, the phantom reference that refers to R is not cleared, although it does not provide any way to access to the referent because PhantomReference::get always returns null. Clearing phantom references like soft and weak references would provide a strong guarantee that it is impossible to access the referent when a phantom reference is enqueued.

      Solution

      Make phantom references be automatically cleared by the garbage collector, like soft and weak references, such that it is impossible to access the referent when its phantom reference is enqueued (since the phantom reference has been cleared).

      Specification

      --- a/src/java.base/share/classes/java/lang/ref/PhantomReference.java
      +++ b/src/java.base/share/classes/java/lang/ref/PhantomReference.java
      @@ -1,5 +1,5 @@
       /*
      - * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
      + * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
        * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
        *
        * This code is free software; you can redistribute it and/or modify it
      @@ -29,23 +29,17 @@
       /**
        * Phantom reference objects, which are enqueued after the collector
        * determines that their referents may otherwise be reclaimed.  Phantom
      - * references are most often used for scheduling pre-mortem cleanup actions in
      - * a more flexible way than is possible with the Java finalization mechanism.
      + * references are most often used to schedule post-mortem cleanup actions.
        *
        * <p> If the garbage collector determines at a certain point in time that the
        * referent of a phantom reference is <a
      - * href="package-summary.html#reachability">phantom reachable</a>, then at that
      - * time or at some later time it will enqueue the reference.
      + * href="package-summary.html#reachability">phantom reachable</a>, then at
      + * that time or at some later time it will clear and enqueue the reference.
        *
        * <p> In order to ensure that a reclaimable object remains so, the referent of
        * a phantom reference may not be retrieved: The {@code get} method of a
        * phantom reference always returns {@code null}.
        *
      - * <p> Unlike soft and weak references, phantom references are not
      - * automatically cleared by the garbage collector as they are enqueued.  An
      - * object that is reachable via phantom references will remain so until all
      - * such references are cleared or themselves become unreachable.
      - *
        * @author   Mark Reinhold
        * @since    1.2
        */
      
      diff --git a/src/java.base/share/classes/java/lang/ref/package-info.java b/src/java.base/share/classes/java/lang/ref/package-info.java
      --- a/src/java.base/share/classes/java/lang/ref/package-info.java
      +++ b/src/java.base/share/classes/java/lang/ref/package-info.java
      @@ -1,5 +1,5 @@
       /*
      - * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
      + * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
        * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
        *
        * This code is free software; you can redistribute it and/or modify it
      @@ -43,8 +43,7 @@
        * implementing memory-sensitive caches, weak references are for
        * implementing canonicalizing mappings that do not prevent their keys
        * (or values) from being reclaimed, and phantom references are for
      - * scheduling pre-mortem cleanup actions in a more flexible way than
      - * is possible with the Java finalization mechanism.
      + * scheduling post-mortem cleanup actions.
        *
        * <p> Each reference-object type is implemented by a subclass of the
        * abstract base {@link java.lang.ref.Reference} class.
      @@ -95,12 +94,10 @@
        * <h3>Automatically-cleared references</h3>
        *
        * Soft and weak references are automatically cleared by the collector
      - * before being added to the queues with which they are registered, if
      - * any.  Therefore soft and weak references need not be registered
      - * with a queue in order to be useful, while phantom references do.
      - * An object that is reachable via phantom references will remain so
      - * until all such references are cleared or themselves become
      - * unreachable.
      + * before being added to the queues with which they are registered, if any,
      + * hence they need not be registered with a queue in order to be useful.
      + * Phantom references, by contrast, do not allow their referents to be
      + * retrieved, so they must be registered with a queue.
        *
        * <a name="reachability"></a>
        * <h3>Reachability</h3>

            mchung Mandy Chung
            mchung Mandy Chung
            Kim Barrett, Mark Reinhold
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: