Most visited

Recently visited

Added in API level 1

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 pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.

每个引用对象类型都由抽象基类Reference类的子类实现。 这些子类之一的实例封装了一个对特定对象的引用 ,称为引用对象 每个引用对象都提供了获取和清除引用的方法。 除了清除操作,引用对象是不可变的,所以没有提供set操作。 程序可以进一步子类化这些子类,为其目的添加所需的任何字段和方法,或者可以不加改变地使用这些子类。

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 add the reference 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 ReferenceQueue class.

注册引用对象与其队列之间的关系是片面的。 也就是说,一个队列不跟踪注册的引用。 如果注册的引用本身变得无法访问,那么它永远不会入队。 程序使用引用对象负责确保只要程序对其对象感兴趣,对象就可以保持可达状态。

虽然有些程序会选择专用线程去除一个或多个队列中的参考对象并对其进行处理,但这绝不是必要的。 经常运作的策略是在执行其他相当频繁的操作过程中检查参考队列。 例如,使用弱引用来实现弱密钥的散列表可以在每次访问表时轮询其引用队列。 这是WeakHashMap类的工作方式。 因为ReferenceQueue.poll方法只是检查内部数据结构,所以此检查会为散列表访问方法增加很少的开销。

Automatically-cleared references

Soft and weak references are automatically cleared by the collector before being added to the queues with which they are registered, if any. Therefore soft and weak references need not be registered with a queue in order to be useful, while phantom references do. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable.

Reachability

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

Classes

PhantomReference<T> 幻象参考对象,它们在收集器之后被排队,并确定它们的对象可能被回收。
Reference<T> 参考对象的抽象基类。
ReferenceQueue<T> 引用队列,在检测到适当的可访问性更改后,垃圾收集器会将已注册的引用对象附加到该引用队列。
SoftReference<T> 软引用对象,由垃圾回收器根据内存需求自行决定清除。
WeakReference<T> 弱参考对象,不妨碍他们的对象被定型,定型,然后再回收。

Hooray!