Name
SoftReference<T>
Synopsis
This class represents a soft reference
to an object. A SoftReference
is not cleared while
there are any remaining hard (direct) references to the referent.
Once the referent is no longer in use (i.e., there are no remaining
hard references to it), the garbage collector may clear the
SoftReference
to the referent at any time.
However, the garbage collector does not clear a
SoftReference
until it determines that system
memory is running low. In particular, the Java VM never throws an
OutOfMemoryError
without first clearing all soft
references and reclaiming the memory of the referents. The VM may
(but is not required to) clear soft references according to a
least-recently-used ordering.
If a SoftReference
has an associated
ReferenceQueue
, the garbage collector enqueues the
SoftReference
at some time after it clears the
reference.
SoftReference
is particularly useful for
implementing object-caching systems that do not have a fixed size,
but grow and shrink as available memory allows.
Figure 10-89. java.lang.ref.SoftReference<T>
public class SoftReference<T> extends Reference<T> { // Public Constructors public SoftReference(T referent); public SoftReference(T referent, ReferenceQueue<? super T> q); // Public Methods Overriding Reference public T get( ); }
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.