JDK-7190310 : Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 7u4
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2012-08-09
  • Updated: 2013-06-26
  • Resolved: 2012-08-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8 Other
7u40Fixed 8Fixed hs24Fixed
Description
Originally stated at http://cs.oswego.edu/pipermail/concurrency-interest/2012-August/009653.html

Running with test:

import java.lang.ref.*;

public class WeakReferenceTest {
  private static Object str = new Object() {
    public String toString() {
      return "The Object";
    }

    protected void finalize() throws Throwable {
      System.out.println("The Object is being finalized");
      super.finalize();
    }
  };
  private final static ReferenceQueue<Object> rq =
      new ReferenceQueue<Object>();
  private final static WeakReference<Object> wr =
      new WeakReference<Object>(str, rq);

  public static void main(String[] args)
      throws InterruptedException {
    Thread reader = new Thread() {
      public void run() {
        while (wr.get() != null) {
        }
        System.out.println("wr.get() returned null");
      }
    };

    Thread queueReader = new Thread() {
      public void run() {
        try {
          Reference<? extends Object> ref = rq.remove();
          System.out.println(ref);
          System.out.println("queueReader returned, ref==wr is "
              + (ref == wr));
        } catch (InterruptedException e) {
          System.err.println("Sleep interrupted - exiting");
        }
      }
    };

    reader.start();
    queueReader.start();

    Thread.sleep(1000);
    str = null;
    System.gc();
  }
}

Output with Server HotSpot:

The Object is being finalized
java.lang.ref.WeakReference@72ebbf5c
queueReader returned, ref==wr is true
 -- VM does not end


Output with Client HotSpot:

The Object is being finalized
wr.get() returned null
java.lang.ref.WeakReference@110769b
queueReader returned, ref==wr is true

--------------------------------------------

The disassembly shows C2 had inlined WeakReference.get(), stored WeakReference.refent in local, and reduced the loop in readerThread to infinite version. Then, readerThread had lost the notification about the WeakReference clearance. 

There are two options with regards to this:
  a. Mark $referent in WeakReference (and other References) as volatile. This will break the C2 hoisting. The downside is requiring volatile write in constructor which might have unexpected performance impact in most code.
  b. Enable C2 to treat $referent as special field being updated by GC, and exempt it from caching in local, re-reading the field every time (or at least each loop iteration, if that is possible with compiler infrastructure).
  c. Other?

Comments
EVALUATION http://hg.openjdk.java.net/hsx/hotspot-comp/hotspot/rev/09aad8452938
20-08-2012