A soft reference holds onto its referent until memory becomes low. : SoftReference « Collections Data Structure « Java






A soft reference holds onto its referent until memory becomes low.

  

import java.lang.ref.SoftReference;

public class Main {
  public static void main(String[] argv) throws Exception {
    SoftReference<String> sr = new SoftReference<String>("object");

    Object o = sr.get();
    if (o != null) {
      System.out.println(o);
    } else {
      System.out.println("collected or has been reclaimed");
    }
  }
}

   
    
  








Related examples in the same category

1.A weak reference is used to determine when an object is no longer being referenced.
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