A weak reference is used to determine when an object is no longer being referenced. : WeakReference « Reflection « Java Tutorial






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

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

    while (true) {
      Reference r = rq.remove();
      if (r == wr) {
        System.out.println("no longer referenced");
      }
    }
  }
}








7.17.WeakReference
7.17.1.A weak reference is used to determine when an object is no longer being referenced.
7.17.2.Testing WeakReference