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

(ref) Reference.clear may extend the lifetime of the referent

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 16
    • 15
    • core-libs
    • None
    • b26

      Reference.clear is presently implemented as just a simple assignment:

        this.referent = null;

      There is a similar assignment in Reference.enqueue.

      For some collectors (notably G1 and Shenandoah, which are SATB collectors), this may unintentionally and undesirably extend the lifetime of the referent, even though the referent object is otherwise unreachable.

      One way to address this would be to make the interpreter, compilers, unsafe, &etc have special knowledge about writes to the referent field, just as they already have special knowledge of reads of that field.

      However, a much simpler to implement solution would be to use a native method to implement clearing the referent. This native method can use the Access API, which provides a GC-agnostic way to perform assignments of such weak references without unnecessarily keeping the object being overwritten alive for longer than needed.

      Neither Reference.clear nor Reference.enqueue seem like performance critical functions, but if necessary the clearing could be intrinsified, using the compiler's Access API mechanisms.

            kbarrett Kim Barrett
            kbarrett Kim Barrett
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: