SoftReference is a Java reference whose referent may be reclaimed when memory is scarce and no strong reference keeps the object alive. It suits caches containing objects that can be recomputed or deserialized again, but it must not be treated as permanent storage. section 9.10
The cache entry can disappear
A softly reachable object may survive several garbage-collection cycles, but an unspecified algorithm can clear it based partly on how long it has existed or been accessed. Code using the cache must therefore handle the referent being unavailable and recreate or reload it. section 9.10
Keep these distinctions explicit:
- Strong references keep an object alive; soft references permit collection when memory demand requires it. section 9.10
- Weakly reachable objects are generally cleared when discovered, whereas softly reachable objects may remain through several collection cycles. section 9.10
- Do not compare different SoftReference instances by equals() expecting their referents to match; SoftReference does not implement content equality. section 7.2
