大猫品Android[三][Reference深入浅出]

本文深入探讨Android中Reference接口的四个子类:SoftReference、WeakReference、PhantomReference和FinalizerReference。重点介绍了它们在内存管理中的角色,特别是如何与ReferenceQueue配合实现对象生命周期的监控。软引用在内存紧张时释放,弱引用无论内存状况如何都会释放,虚引用不直接影响对象生命周期,主要用于跟踪对象状态。FinalizerReference协助FinalizerDaemon守护线程完成对象的finalize操作。理解这些概念对于优化Android应用内存使用至关重要。
摘要由CSDN通过智能技术生成

写在前面: 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> {
   

     ...

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值