Class Overview
A reference that is cleared when its referent is not strongly reachable and
there is memory pressure.
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.
Garbage Collection of Soft References
When the garbage collector encounters an object
obj
that is
softly-reachable, the following happens:
- A set
refs
of references is determined. refs
contains
the following elements:
- All soft references pointing to
obj
.
- All soft references pointing to objects from which
obj
is
strongly reachable.
- All references in
refs
are atomically cleared.
- At the same time or some time in the future, all references in
refs
will be enqueued with their corresponding reference queues, if
any.
The system may delay clearing and enqueueing soft references, yet all
SoftReference
s pointing to softly reachable objects will be cleared before
the runtime throws an
OutOfMemoryError
.
Unlike a WeakReference
, a SoftReference
will not be
cleared and enqueued until the runtime must reclaim memory to satisfy an
allocation.
Summary
[Expand]
Inherited Methods |
From class
java.lang.ref.Reference
void
|
clear()
Makes the referent null .
|
boolean
|
enqueue()
Forces the reference object to be enqueued if it has been associated with
a queue.
|
T
|
get()
Returns the referent of the reference object.
|
boolean
|
isEnqueued()
Checks whether the reference object has been enqueued.
|
|
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals(Object o)
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()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
final
void
|
wait(long millis, 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 millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
|
Public Constructors
public
SoftReference
(T r)
Constructs a new soft reference to the given referent. The newly created
reference is not registered with any reference queue.
public
SoftReference
(T r, ReferenceQueue<? super T> q)
Constructs a new soft reference to the given referent. The newly created
reference is registered with the given reference queue.
Parameters
r
| the referent to track |
q
| the queue to register to the reference object with. A null value
results in a weak reference that is not associated with any
queue.
|