SoftReference
Stay organized with collections
Save and categorize content based on your preferences.
Soft reference objects, which are cleared at the discretion of the garbage
collector in response to memory demand.
Suppose that the garbage collector determines at a certain point in time
that an object is softly
reachable. At that time it may choose to clear atomically all soft
references to that object and all soft references to any other
softly-reachable objects from which that object is reachable through a chain
of strong references. At the same time or at some later time it will
enqueue those newly-cleared soft references that are registered with
reference queues.
All soft references to softly-reachable objects are guaranteed to have
been cleared before the virtual machine throws an
OutOfMemoryError
. Otherwise no constraints are placed upon the
time at which a soft reference will be cleared or the order in which a set
of such references to different objects will be cleared. Virtual machine
implementations are, however, encouraged to bias against clearing
recently-created or recently-used soft references.
Avoid Soft References for Caching
In practice, soft references are inefficient for caching. The runtime doesn't
have enough information on which references to clear and which to keep. Most
fatally, it doesn't know what to do when given the choice between clearing a
soft reference and growing the heap.
The lack of information on the value to your application of each reference
limits the usefulness of soft references. References that are cleared too
early cause unnecessary work; those that are cleared too late waste memory.
Most applications should use an android.util.LruCache
instead of
soft references. LruCache has an effective eviction policy and lets the user
tune how much memory is allotted.
Public Constructor Summary
|
SoftReference(T referent)
Creates a new soft reference that refers to the given object.
|
|
SoftReference(T referent, ReferenceQueue<? super T> q)
Creates a new soft reference that refers to the given object and is
registered with the given queue.
|
Inherited Method Summary
From class
java.lang.ref.Reference
void
|
clear()
Clears this reference object.
|
boolean
|
enqueue()
Adds this reference object to the queue with which it is registered,
if any.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
T
|
get()
Returns this reference object's referent.
|
boolean
|
isEnqueued()
Tells whether or not this reference object has been enqueued, either by
the program or by the garbage collector.
|
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals( Object obj)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long timeout, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long timeout)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
Public Constructors
public
SoftReference
(T referent)
Creates a new soft reference that refers to the given object. The new
reference is not registered with any queue.
Parameters
referent |
object the new soft reference will refer to
|
public
SoftReference
(T referent, ReferenceQueue<? super T> q)
Creates a new soft reference that refers to the given object and is
registered with the given queue.
Parameters
referent |
object the new soft reference will refer to |
q |
the queue with which the reference is to be registered,
or null if registration is not required
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["`SoftReference` objects are garbage collected based on memory demand, potentially clearing them before an `OutOfMemoryError`."],["Soft references are generally not suitable for caching due to unpredictable clearing behavior, `android.util.LruCache` is recommended instead."],["A `SoftReference` can be created with or without registering it with a `ReferenceQueue` for tracking cleared references."],["`SoftReference` provides methods inherited from `Reference` to interact with the referenced object, such as `get()` to retrieve it and `clear()` to manually clear the reference."]]],[]]