Create a soft reference in Java

Description

The following code shows how to create a soft reference.

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

Example


/*from   w w  w  .j a v a2 s.  c  o m*/
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");
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization