Name
WeakReference<T>
Synopsis
This class refers to an object in a way
that does not prevent that referent object from being finalized and
reclaimed by the garbage collector. When the garbage collector
determines that there are no more hard (direct) references to the
object, and that there are no SoftReference
objects that refer to the object, it clears the
WeakReference
and marks the referent object for
finalization. At some point after this, it also enqueues the
WeakReference
on its associated
ReferenceQueue
, if there is one, in order to
provide notification that the referent has been reclaimed.
WeakReference
is used by
java.util.WeakHashMap
to implement a hashtable that does not
prevent the hashtable key object from being garbage-collected.
WeakHashMap
is useful when you want to associate
auxiliary information with an object but do not want to prevent the
object from being reclaimed.
Figure 10-90. java.lang.ref.WeakReference<T>
public class WeakReference<T> extends Reference<T> { // Public Constructors public WeakReference(T referent); public WeakReference(T referent, ReferenceQueue<? super T> q); }
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.