A phantom reference is used to determine when an object is just about to be reclaimed. : PhantomReference « Reflection « Java Tutorial






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

public class Main {
  public static void main(String[] argv) throws Exception {
    ReferenceQueue rq = new ReferenceQueue();
    PhantomReference<String> pr = new PhantomReference<String>("object", rq);

    while (true) {
      Reference r = rq.remove();
      if (r == pr) {
        // about to be reclaimed.
        r.clear();
      }
    }
  }
}








7.15.PhantomReference
7.15.1.A phantom reference is used to determine when an object is just about to be reclaimed.
7.15.2.Testing PhantomReference