Top Description Classes
module java.base

Package java.lang.ref


Provides reference-object classes, which support a limited degree of interaction with the garbage collector. A program may use a reference object to maintain a reference to some other object in such a way that the latter object may still be reclaimed by the collector. A program may also arrange to be notified some time after the collector has determined that the reachability of a given object has changed.

Package Specification

A reference object encapsulates a reference to some other object so that the reference itself may be examined and manipulated like any other object. Three types of reference objects are provided, each weaker than the last: soft, weak, and phantom. Each type corresponds to a different level of reachability, as defined below. Soft references are for implementing memory-sensitive caches, weak references are for implementing canonicalizing mappings that do not prevent their keys (or values) from being reclaimed, and phantom references are for scheduling post-mortem cleanup actions. Post-mortem cleanup actions can be registered and managed by a java.lang.ref.Cleaner.

Each reference-object type is implemented by a subclass of the abstract base java.lang.ref.Reference class. An instance of one of these subclasses encapsulates a single reference to a particular object, called the referent. Every reference object provides methods for getting and clearing the reference. Aside from the clearing operation reference objects are otherwise immutable, so no set operation is provided. A program may further subclass these subclasses, adding whatever fields and methods are required for its purposes, or it may use these subclasses without change.

Notification

A program may request to be notified of changes in an object's reachability by registering an appropriate reference object with a reference queue at the time the reference object is created. Some time after the garbage collector determines that the reachability of the referent has changed to the value corresponding to the type of the reference, it will clear the reference and add it to the associated queue. At this point, the reference is considered to be enqueued. The program may remove references from a queue either by polling or by blocking until a reference becomes available. Reference queues are implemented by the java.lang.ref.ReferenceQueue class.

The relationship between a registered reference object and its queue is one-sided. That is, a queue does not keep track of the references that are registered with it. If a registered reference becomes unreachable itself, then it will never be enqueued. It is the responsibility of the program using reference objects to ensure that the objects remain reachable for as long as the program is interested in their referents.

While some programs will choose to dedicate a thread to removing reference objects from one or more queues and processing them, this is by no means necessary. A tactic that often works well is to examine a reference queue in the course of performing some other fairly-frequent action. For example, a hashtable that uses weak references to implement weak keys could poll its reference queue each time the table is accessed. This is how the java.util.WeakHashMap class works. Because the ReferenceQueue.poll method simply checks an internal data structure, this check will add little overhead to the hashtable access methods.

Reachability

Going from strongest to weakest, the different levels of reachability reflect the life cycle of an object. They are operationally defined as follows:
Author
Mark Reinhold
Since
1.2

Class Summary

Modifier and TypeClass and Description
public class
Cleaner

Cleaner manages a set of object references and corresponding cleaning actions.

pack-priv class
pack-priv class
FinalizerHistogram

This FinalizerHistogram class is for GC.finalizer_info diagnostic command support.

pack-priv class
FinalReference<T>

Final references, used to implement finalization

pack-priv class
NativeReferenceQueue<T>

An implementation of a ReferenceQueue that uses native monitors.

public class
PhantomReference<
the type of the referent
T
>

Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed.

public abstract class
Reference<
the type of the referent
T
>

Abstract base class for reference objects.

public class
ReferenceQueue<
the type of the reference object
T
>

Reference queues, to which registered reference objects are appended by the garbage collector after the appropriate reachability changes are detected.

public class
SoftReference<
the type of the referent
T
>

Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand.

public class
WeakReference<
the type of the referent
T
>

Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed.