import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Arthur Edelstein, arthuredelstein@gmail.com, 2011.01.04
 * UCSF
 * 
 * This program lets you test the effects of different JVM command line
 * settings on SoftReferences. We create 1 MB objects every 100 ms
 * and hold them in SoftReferences.
 * 
 * Example run command:
 * java  -Xmx1000M -server -XX:SoftRefLRUPolicyMSPerMB=1000000000000000 SoftReferenceTest 800
 * 1 GB maximum heap, server mode, only garbage collect SoftRefs when necessary, allocate 800 1-MB objects.
 */
public class SoftReferenceTest {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
      final int MBsize = 1024*1024;
      final double MB_d = (double) MBsize;
      Runtime r = Runtime.getRuntime();
      List myList<SoftReference> = new ArrayList<SoftReference>();
      int n = Integer.parseInt(args[0]);
      long t0 = System.currentTimeMillis();
      long t;
      for (int i = 0; ;) {
         if (i<n) {
            SoftReference x<byte[]> = new SoftReference<byte []>(new byte[MBsize]);
            myList.add(x);
            ++i;
         }
         t = System.currentTimeMillis() - t0;
         try {
            Thread.sleep(100);
         } catch (InterruptedException ex) {}
         System.out.format("t: %.2f\tallocated: %.2f\tused: %.2f\theap: %.2f\tmax: %.2f\n", t/(double) 1000., (double) i, (r.totalMemory()-r.freeMemory())/MB_d, r.totalMemory() / MB_d, r.maxMemory() / MB_d);
      }
   }
}

@@ -831,23 +831,16 @@
     List
- myList
 %3CSoftRef
@@ -846,16 +846,23 @@
 ference%3E
+ myList
  = new A
@@ -925,24 +925,66 @@
 t(args%5B0%5D);%0A
+      int dt = Integer.parseInt(args%5B1%5D);%0A
       long t
@@ -1105,18 +1105,16 @@
 ence
- x
 %3Cbyte%5B%5D%3E
  = n
@@ -1109,16 +1109,18 @@
 %3Cbyte%5B%5D%3E
+ x
  = new S
@@ -1301,19 +1301,18 @@
 d.sleep(
-100
+dt
 );%0A     

allow different speeds