Package java.lang.ref
Class WeakReference<T>
- java.lang.Object
-
- java.lang.ref.Reference<T>
-
- java.lang.ref.WeakReference<T>
-
public class WeakReference<T> extends Reference<T>
Implements a weak reference, which is the middle of the three types of references. Once the garbage collector decides that an objectobjis is weakly-reachable, the following happens:-
A set
refof references is determined.refcontains the following elements:-
All weak references pointing to
obj. -
All weak references pointing to objects from which
objis either strongly or softly reachable.
-
All weak references pointing to
-
All references in
refare atomically cleared. -
All objects formerly being referenced by
refbecome eligible for finalization. -
At some future point, all references in
refwill be enqueued with their corresponding reference queues, if any.
SoftReferenceand aWeakReferenceis the point of time at which the decision is made to clear and enqueue the reference:-
A
SoftReferenceshould be cleared and enqueued as late as possible, that is, in case the VM is in danger of running out of memory. -
A
WeakReferencemay be cleared and enqueued as soon as is known to be weakly-referenced.
-
A set
-
-
Field Summary
-
Fields inherited from class java.lang.ref.Reference
pendingNext
-
-
Constructor Summary
Constructors Constructor Description WeakReference(T r)Constructs a new weak reference to the given referent.WeakReference(T r, ReferenceQueue<? super T> q)Constructs a new weak reference to the given referent.
-
-
-
Constructor Detail
-
WeakReference
public WeakReference(T r)
Constructs a new weak reference to the given referent. The newly created reference is not registered with any reference queue.- Parameters:
r- the referent to track
-
WeakReference
public WeakReference(T r, ReferenceQueue<? super T> q)
Constructs a new weak reference to the given referent. The newly created reference is registered with the given reference queue.- Parameters:
r- the referent to trackq- the queue to register to the reference object with. A null value results in a weak reference that is not associated with any queue.
-
-