This article gave me a good understanding of each of them (weak, soft and phantom references). Here's a summarized cite:
A weak reference, simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself.
A soft reference is exactly like a weak reference, except that it is less eager to throw away the object to which it refers. An object which is only weakly reachable (the strongest references to it are WeakReferences
) will be discarded at the next garbage collection cycle, but an object which is softly reachable will generally stick around for a while.
A phantom reference is quite different than either SoftReference
or WeakReference
. Its grip on its object is so tenuous that you can't even retrieve the object -- its get()
method always returns null
. The only use for such a reference is keeping track of when it gets enqueued into a ReferenceQueue
, as at that point you know the object to which it pointed is dead.