A phantom reference is used to determine when an object is just about to be reclaimed. : SoftReference « Collections Data Structure « Java






A phantom reference is used to determine when an object is just about to be reclaimed.

  


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();
      }
    }
  }
}

   
    
  








Related examples in the same category

1.A soft reference holds onto its referent until memory becomes low.
2.A weak reference is used to determine when an object is no longer being referenced.
3.Testing SoftReference
4.Testing WeakReference
5.Testing PhantomReference
6.Soft ValueMap
7.An implementation of Set that manages a map of soft references to the set values.
8.Cache based on SoftReference