Weak, soft, phantom references
SoftReference, WeakReference, and PhantomReference point at an object without being ordinary strong references. The collector can reclaim the referent under different rules. You use them for caches, canonical maps, and cleanup hooks.
Detailed Explanation
A normal field is a strong reference. As long as it is reachable, the object is live. A WeakReference does not keep the referent alive. After a GC decides the object is only weakly reachable, it clears the weak reference and optionally enqueues it on a ReferenceQueue. WeakHashMap is built on this: keys vanish when callers drop them.
A SoftReference is for caches that should survive a young GC but yield under memory pressure. The collector clears soft refs before throwing OutOfMemoryError, preferring to keep recently used ones (implementation policy). They are not a substitute for a bounded cache. They can linger and still blow the heap if the live strong set is large.
A PhantomReference cannot be used to retrieve the object (get is null). It enqueues after the object is phantom-reachable, which is after finalization would have run. Use it (or Cleaner) to run cleanup after the object is dead, without resurrecting it. finalize is the old, unsafe version of this idea.
ReferenceQueue is how you learn that a referent died. A background thread polls the queue and drops map entries or closes a native handle. If you never poll, the Reference objects themselves leak.
You reach for weak refs when the extra data should die with the key. You reach for soft refs only after a bounded cache was not enough and you accept GC-driven eviction. You reach for phantom/Cleaner for native resources. You keep strong refs for anything you must not lose.
The cost of a soft cache of huge values is an unpredictable heap. The cost of a weak map whose values point at keys is a leak. The cost of never draining the queue is a leak of Reference objects. Do not use these types for application flow control; they are GC-timed, not business-timed.
Code Examples
import java.lang.ref.WeakReference;
public class Weak {
public static void main(String[] args) {
Object obj = new Object();
WeakReference<Object> weak = new WeakReference<>(obj);
System.out.println(weak.get() != null); // prints: true
obj = null;
System.gc();
// get() may now be null; we only show the API
System.out.println(weak instanceof WeakReference); // prints: true
}
}import java.lang.ref.SoftReference;
public class Soft {
public static void main(String[] args) {
SoftReference<byte[]> cache = new SoftReference<>(new byte[1024]);
byte[] hit = cache.get();
System.out.println(hit != null && hit.length == 1024); // prints: true
}
}Common Gotchas
Values that reference weak keys pin the keys
`WeakHashMap` entries never clear.
Soft references are not a size-bounded cache
they evict late and unpredictably.
`PhantomReference.get()` is always null
cleanup goes through the queue or `Cleaner`.
Ignoring a `ReferenceQueue` leaks `Reference` objects
Ignoring a `ReferenceQueue` leaks `Reference` objects