Could u help me please with understanding PhantomReference? I know that PhantomReference help with tracking when object was removed from heap, and is reachable after finalize method is called. I have tried to get my hands dirty with some code but I cant get it right e.g
class Foo{
private String a;
public Foo(String a){
this.a = a;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println("calling finalize");
}
@Override
public String toString() {
return "Foo{" + "a=" + a + '}';
}
}
I thought that doing something like this:
ReferenceQueue q = new ReferenceQueue();
PhantomReference<Foo> pr = new PhantomReference(new Foo("myphantom"), q);
System.out.println("Object created trying to gc");
System.gc();
Thread.sleep(5000L);
System.out.println(q.poll());
Will give me instance of PhantomReference, but what i got is null.