Holding onto an Object Until Memory Becomes Low - Java Java Virtual Machine

Java examples for Java Virtual Machine:Utility

Description

Holding onto an Object Until Memory Becomes Low

Demo Code

import java.awt.Graphics;
import java.lang.ref.SoftReference;

public class Main {
  public void m() {
    // Create up the soft reference.
    Object object = "";
    SoftReference sr = new SoftReference(object);

    // Use the soft reference.
    Object o = sr.get();/* ww w. j  av a 2 s .c  o  m*/
    if (o != null) {
     // process(o);
    } else {
      // The object is being collected or has been reclaimed.
    }
  }
}

Related Tutorials