A weak reference is used to determine when an object is no longer being referenced. : SoftReference « Collections Data Structure « Java






A weak reference is used to determine when an object is no longer being referenced.

  

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

   
    
  








Related examples in the same category

1.A soft reference holds onto its referent until memory becomes low.
2.A phantom reference is used to determine when an object is just about to be reclaimed.
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