One distinction to be clear on is the difference between a WeakReference
and a SoftReference
.
Basically a WeakReference
will be GC-d by the JVM eagerly, once the referenced object has no hard references to it. A SoftReference
d object on the other hand, will tend to be left about by the garbage collector until it really needs to reclaim the memory.
A cache where the values are held inside WeakReference
s would be pretty useless (in a WeakHashMap
, it is the keys which are weakly referenced). SoftReferences
are useful to wrap the values around when you want to implement a cache which can grow and shrink with the available memory
Read full article from java - When would you use a WeakHashMap or a WeakReference? - Stack Overflow
No comments:
Post a Comment