写在前面: Reference本身是一个接口,表示一个引用,不能直接使用,有四个它的派生类供我们使用,它们分别是:SoftReference,WeakReference,PhantomReference,FinalizerReference .其中SoftReference,WeakReference和 PhantomReference的区别与使用Google一下已经有大把的介绍资料,因此本文对此只简单说明带过,主要给大家介绍你不知道的Reference.
一. SoftReference
SoftReference表示一个对象的软引用, SoftReference所引用的对象在发生GC时,如果该对象只被这个SoftReference所引用,那么在内存使用情况已经比较紧张的情况下会释放其所占用的内存,若内存比较充实,则不会释放其所占用的内存.比较常用于一些Cache的实现.
其构造函数中允许传入一个ReferenceQueue.其代码如下所示:
SoftReference.java
public class SoftReference<T> extends Reference<T> {
/**
* Constructs a new soft reference to the given referent. The newly created
* reference is not registered with any reference queue.
*
* @param r the referent to track
*/
public SoftReference(T r) {
super(r, null);
}
/**
* Constructs a new soft reference to the given referent. The newly created
* reference is registered with the given reference queue.
*
* @param r the referent to track
* @param 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.
*/
public SoftReference(T r, ReferenceQueue<? super T> q) {
super(r, q);
}
}
这个ReferenceQueue才是本文重点之一,后面会专门说到.
二.WeakReference
WeakReference表示一个对象的弱引用,WeakReference所引用的对象在发生GC时,如果该对象只被这个WeakReference所引用,那么不管当前内存使用情况如何都会释放其所占用的内存.
其构造函数中允许传入一个ReferenceQueue.这个ReferenceQueue才是本文重点之一,后面会专门说到.WeakReference与SoftReference一样派生于Reference类:
WeakReference.java
public class WeakReference<T> extends Reference<T> {
/**
* Constructs a new weak reference to the given referent. The newly created
* reference is not registered with any reference queue.
*
* @param r the referent to track
*/
public WeakReference(T r) {
super(r, null);
}
/**
* Constructs a new weak reference to the given referent. The newly created
* reference is registered with the given reference queue.
*
* @param r the referent to track
* @param 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.
*/
public WeakReference(T r, ReferenceQueue<? super T> q) {
super(r, q);
}
}
三. PhantomReference
PhantomReference表示一个虚引用, 说白了其无法引用一个对象,即对对象的生命周期没有影响.
其代码如下:
PhantomReference.java
public class PhantomReference<T> extends Reference<T> {
/**
* Constructs a new phantom reference and registers it with the given
* reference queue. The reference queue may be {
@code null}, but this case
* does not make any sense, since the reference will never be enqueued, and
* the {
@link #get()} method always returns {
@code null}.
*
* @param r the referent to track
* @param q the queue to register the phantom reference object with
*/
public PhantomReference(T r, ReferenceQueue<? super T> q) {
super(r, q);
}
/**
* Returns {
@code null}. The referent of a phantom reference is not
* accessible.
*
* @return {
@code null} (always)
*/
@Override
public T get() {
return null;
}
}
可以看到他重写了Reference的get方法直接返回null.所以说它并不是为了改变某个对象的生命周期而存在的.它常用于跟踪某些对象的生命周期状态,它只有一个接受ReferenceQueue的构造方法.正是这个ReferenceQueue的神奇功效帮助PhantomReference实现了跟踪对象生命周期的功能.这里忍不住再一次铺垫,ReferenceQueue马上就来.
四.ReferenceQueue
在介绍 ReferenceQueue 之前,先关注下前面介绍的三个引用类的共同的父类Reference.
Reference.java
public abstract class Reference<T> {
...