我所知道的对象引用

577 阅读4分钟

碎碎念

JDK1.2之后对对象的引用进行了划分,分别存在有强引用,软引用,弱引用,虚引用

  1. 强引用(Strong Reference)

强引用是最普通的引用,如果一个对象是强引用。那么垃圾回收器绝对不会回收它。当内存空间不足时,java虚拟机会抛出OutOfMemoryError是程序异常终止,但是不会回收强引用对象。

  1. 软引用(Soft Reference)

一个对象如果是软引用的话,那么当程序内存不足的时候,垃圾回收器会回收它。那内存空间不紧张的时候。它就一直存在。也就意味着我们是使用它。

  1. 弱引用(Weak Reference)

弱引用相比于软引用它的声明周期更短。当垃圾回收器在工作的过程中,发现了弱引用对象,不管当前内存是否紧张。都会将其回收。但是GC的触发不是特别的频繁。所以不会那么快发现这个引用。

  1. 虚引用 (Phantom Reference)

虚引用顾名思义,就是形同虚设,与其他几种引用都不同,虚引用并不会决定对象的生命周期。

设计一个软引用缓存集合

由于种种因素,我们需要缓存一些数据在内存中,假设我们现在需要缓存的数据是Person类。( 注意Person对象很大,像BitMap一样)


public class Person {
    private String name;
    private byte[] datas = new byte[1024*1024*100];

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

正常情况下,我们是直接使用如下的代码


public class Main {

    public static void main(String[] args) {

        Map<Integer, Person> pp = new HashMap<>();
        for (int i = 0; i < 100; i++) {
            Person person = new Person("hello world");
            pp.put(i, person);
        }

        for (int i = 0; i < 100; i++) {
            System.out.println(pp.get(i));
        }
    }
}

但是一运行,你就会发现出现如下的错误

image.png

因为Person的数据量太大了,导致JVM出现了OOM。那么有没有办法容纳下这么多的数据,而不会出现OOM呢? 嗯~ 没有办法。 因为JVM内存是有限制的,你没有办法无节制的往内存中存放东西。但是我们可以换个角度,来思考。尽可能的利用JVM内存呢?嗯~ 可以的,我们可以利用上面所说的软引用特性来设计一个缓存集合。



/***
 * 软引用应用设计集合
 * @param <K> Key
 * @param <V> Value
 */
public class SoftReferenceCache<K, V> {

    private Map<K, SoftReference<V>> mCache = new HashMap<>();


    /**
     * 存放数据
     *
     * @param k 数据Key
     * @param v 数据Value
     */
    public void put(K k, V v) {
        mCache.put(k, new SoftReference<V>(v));
    }


    /**
     * 存放数据
     *
     * @param k 数据Key
     * @return 返回数据Value
     */
    public V get(K k) {
        SoftReference<V> reference = mCache.get(k);
        if (reference != null) {
            return reference.get();
        }
        return null;
    }

}

然后使用上面的 SoftReferenceCache 来进行存储

 public static void main(String[] args) {
        SoftReferenceCache<Integer, Person> softReferenceCache = new SoftReferenceCache<>();
        for (int i = 0; i < 100; i++) {
            softReferenceCache.put(i, new Person("hello world" + i));
        }

        for (int i = 0; i < 100; i++) {
            System.out.println(softReferenceCache.get(i));
        }
    }

运行后,你会发现不会出现OOM。但是会存在有被GC回收的对象。这个时候,你需要在自己的逻辑上增加三级缓存

image.png

如何知道一个对象被GC回收呢?

我们想要知道一个对象什么时候会被GC回收。就需要利用引用队列ReferenceQueue,GC在准备回收一个对象时,如果发现它还仅有软引用(或弱引用,或虚引用)指向它,就会在回收该对象之前,把这个软引用(或弱引用,或虚引用)加入到与之关联的引用队列(ReferenceQueue)中,这样我们就知道,对象被GC回收了。

 public static void main(String[] args) throws InterruptedException {
        ReferenceQueue<Person> queue = new ReferenceQueue<>();
        WeakReference<Person> cc = new WeakReference<>(new Person("hello"), queue);
        System.gc();
        Thread.sleep(2000);
        Object object;
        while ((object = queue.poll()) != null) {
            System.out.println("GC回收了>>>>:" + object);
        }
    }

上述代码,可以知道cc什么时候被回收

image.png

Android 中的 Reference

  1. Android 不建议使用软引用来做缓存,因为Android Runtime无法感知,应该通过GC来回收内存,还是增加APP的栈内存。官方推荐我们是LruCache

image.png

  1. 当我们能够了解一个对象的销毁时机,就意味着我们能对一些本该销毁的对象进行反应。 也就是内存泄露的检测。LeakCanary 底层就是使用引用队列进行对ActivityFragment进行检测的。

相关连接

  1. developer.android.com/reference/j…
  2. github.com/square/leak…

原文地址:www.jianshu.com/p/63b907aa0…