Code example for SoftReference

Methods: clearget

0
 
  public static void main(String... args) throws Exception {
    ClassLoader cl = ClassUnloadCommon.newClassLoader();
    Class<?> c = cl.loadClass(className);
    Object o = c.newInstance();
    SoftReference<Object> sr = new SoftReference(o);
    o = null; c = null; cl = null;
 
    { 
        boolean isAlive = wb.isClassAlive(className);
        System.out.println("testSoftReference (1) alive: " + isAlive);
        boolean cleared = (sr.get() == null);
        boolean shouldBeAlive = !cleared;
        ClassUnloadCommon.failIf(isAlive != shouldBeAlive, "" + isAlive + " != " + shouldBeAlive);
    } 
 
    ClassUnloadCommon.triggerUnloading(); 
 
    { 
        boolean isAlive = wb.isClassAlive(className);
        System.out.println("testSoftReference (2) alive: " + isAlive);
        boolean cleared = (sr.get() == null);
        boolean shouldBeAlive = !cleared;
        ClassUnloadCommon.failIf(isAlive != shouldBeAlive, "" + isAlive + " != " + shouldBeAlive);
    } 
    sr.clear();
    ClassUnloadCommon.triggerUnloading(); 
 
    { 
        boolean isAlive = wb.isClassAlive(className);
        System.out.println("testSoftReference (3) alive: " + isAlive);
        boolean cleared = (sr.get() == null);
        boolean shouldBeAlive = !cleared;
        ClassUnloadCommon.failIf(isAlive != shouldBeAlive, "" + isAlive + " != " + shouldBeAlive);
    } 
  } 
}