Skip to content

Most visited

Recently visited

navigation
Added in API level 1

SoftReference

public class SoftReference
extends Reference<T>

java.lang.Object
   ↳ java.lang.ref.Reference<T>
     ↳ java.lang.ref.SoftReference<T>


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: The system may delay clearing and enqueueing soft references, yet all SoftReferences 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

Public constructors

SoftReference(T r)

Constructs a new soft reference to the given referent.

SoftReference(T r, ReferenceQueue<? super T> q)

Constructs a new soft reference to the given referent.

Inherited methods

From class java.lang.ref.Reference
From class java.lang.Object

Public constructors

SoftReference

Added in API level 1
SoftReference (T r)

Constructs a new soft reference to the given referent. The newly created reference is not registered with any reference queue.

Parameters
r T: the referent to track

SoftReference

Added in API level 1
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 T: the referent to track
q ReferenceQueue: the queue to register to the reference object with. A null value results in a weak reference that is not associated with any queue.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.