Reference types
- soft: for caching
- weak: for fast cleanup (pre-finalizer)
- phantom: for safe cleanup (post-finalizer)
Reference queues: for notifications
Soft references: for quick-and-dirty caching only
Cleared when the VM runs low on memory
Hopefully in LRU fashion
Tuned with -XX:SoftRelLRUPolicyMSPerMB
how long to retain soft refs in ms per free MB of heap
Default: 1000ms
Soft refs have no notion of weight:
- memory usage
- computation time
- cpu usage
volatile SoftReference<byte[]> dataReference = new SoftReference<byte[]>(null); //这个地方是可以写null的
public byte[] getData(){
byte[] data = dataReference.get();
if(data !=null) return data;
data = readData();
dataReference = new SoftReference<byte[]>(data);
return data;
}
//这些代码totally没有意思,os会自己做cache的。
Weak reference
cleared as soon as no strong and soft remains
cleared ASAP, before the finalizer runs
Not for caching. Use soft reference, as intended. check jvm spec.
Phantom reference
get() always returns null.// gc runs concurrently with your code
so you must use in a reference queue.
java.util.WeakHashMap
useful for emulating additional fields
keeps weak refs to keys, strong refs to values
not concurrent
uses equals() when it should use ==
Recap:
- start at a root
- trace and mark strongly-referenced objects
- optionally clear soft references
- trace and mark softly-referenced objects
- clear weak references
- enqueue finalizable objects
- repeast steps 1 through 5 for the queue
- possibly enqueue phantom references
- the remaining objects are dead
- repeat
public class GetterMethods{
final static Cache<class<?>,List<Method>> cache = CacheBuilder.newBuilder()
.weakKeys()
.softValues()
.build(new CacheLoader<Class<?>,List<Method>>(){
public List<Method> load(<Class<?> clazz){
List<Method> getters = new ArrayList<Method>();
for(Method m: clazz.getMethods())
if(m.getName().startsWith("get"))
getters.add(m);
return getters;
}
});
public static List<Method> on(Class<?> clazz){
return cache.apply(clazz);
}
//Usage:
//List<Method> l = GetterMethods.on(Foo.class);
}
To dynamically reload your web application, you need to be careful not unintentionally to retain your app classloader from being collected.