Phantom referenced objects may not appear in the ReferenceQueue if VM run with -Xcomp.
The minimal test demonstrating the bug looks like:
--- 8< ------- Phantom.java --------------------
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
public class Phantom {
static ReferenceQueue rq = new ReferenceQueue();
public static void main(final String[] args) throws Exception {
PhantomReference ref = new PhantomReference(new Object(), rq);
System.out.println("ref: " + ref);
System.gc();
Reference rmRef = rq.remove(1000);
if (rmRef == null) {
for (int i = 0; i < 10; i++) {
System.gc();
}
rmRef = rq.remove(1000);
if (rmRef == null) {
throw new Error("Test failed");
}
}
System.out.println("Test passed");
}
}
--- 8< ---------------------------------------------------------
# java Phantom
ref: java.lang.ref.PhantomReference@614c5515
Test passed
# java -Xcomp Phantom
ref: java.lang.ref.PhantomReference@614c5515
Exception in thread "main" java.lang.Error: Test failed
at Phantom.main(Phantom.java:19)
Please note: Declaring 'PhantomReference ref' as a static member of the Phantom class, not as a local variable, will make the test pass.
The minimal test demonstrating the bug looks like:
--- 8< ------- Phantom.java --------------------
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
public class Phantom {
static ReferenceQueue rq = new ReferenceQueue();
public static void main(final String[] args) throws Exception {
PhantomReference ref = new PhantomReference(new Object(), rq);
System.out.println("ref: " + ref);
System.gc();
Reference rmRef = rq.remove(1000);
if (rmRef == null) {
for (int i = 0; i < 10; i++) {
System.gc();
}
rmRef = rq.remove(1000);
if (rmRef == null) {
throw new Error("Test failed");
}
}
System.out.println("Test passed");
}
}
--- 8< ---------------------------------------------------------
# java Phantom
ref: java.lang.ref.PhantomReference@614c5515
Test passed
# java -Xcomp Phantom
ref: java.lang.ref.PhantomReference@614c5515
Exception in thread "main" java.lang.Error: Test failed
at Phantom.main(Phantom.java:19)
Please note: Declaring 'PhantomReference ref' as a static member of the Phantom class, not as a local variable, will make the test pass.
- duplicates
-
JDK-8168682 jdk/test/java/lang/ClassLoader/forNameLeak/ClassForNameLeak.java fails with -Xcomp: ClassLoader was never enqueued!
-
- Resolved
-