Question

Demo code:

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Object test = new Object();
        ReferenceQueue<Object> q = new ReferenceQueue<Object>();
        PhantomReference<Object> p = new PhantomReference<Object>(test, q);
        Object lock = new Object();
        while (true) {
            synchronized (lock) {
                                 //q.poll() is null always,why?
                if (q.poll() != null) {
                    break;
                }
                //System.gc();
                lock.wait();
            }
        }
        System.out.println(1111111);
    }
}

I tested the code,but it always is dead loop. The code (System.out.println(1111111);) can not execute,q.poll() reurn null.

I think if test object is removed by GC,q.poll() will return p object,then break loop,but invoke this demo code,it is not like my thought

Edited: I modify the demo code,it can work now.

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Object test = new Object();
        ReferenceQueue<Object> q   = new ReferenceQueue<Object>();
        PhantomReference<Object> p = new PhantomReference<Object>(test, q);
        Object lock = new Object();
        while (true) {
            synchronized (lock) {
                if (q.poll() != null) {
                    break;
                }
                test = null; //it is important  
                System.gc();
                lock.wait(100);//should not lock.wait()
            }
        }
        System.out.println(1111111);
    }
}

AS sb says,the statement( test=null) is key.GC collect test object that assign null.

Was it helpful?

Solution

The fact that the test variable is still present means the GC will never collect the object it refers to... I believe JVMs don't collect anything that a local variable refers to even if the local variable is never referred to in the rest of the method.

(Also, it's not clear why you're using lock.wait() when nothing is calling lock.pulse().)

Here's code which works for me:

import java.lang.Thread;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        Object test = new Object();
        ReferenceQueue<Object> q = new ReferenceQueue<Object>();
        PhantomReference<Object> p = new PhantomReference<Object>(test, q);
        Object lock = new Object();
        while (true) {
            System.out.println("Checking queue...");
            if (q.poll() != null) {
                break;
            }
            System.out.println("Still polling...");
            System.gc();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                System.out.println("Interrupted!");
                break;
            }
            // Make the object garbage...
            test = null;
        }
        System.out.println("Finished.");
    }
}

OTHER TIPS

Try

test = null;

before your while loop, so that the test object is eligible for garbage collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top