Java&Android 基础知识梳理(9) - LruCache 源码解析

一、基本概念

1.1 LruCache 的作用

LruCache的基本思想是Least Recently Used,即 最近最少使用,也就是当LruCache内部缓存在内存中的对象大小之和到达设定的阈值后,会删除 访问时间距离当前最久 的对象,从而避免了OOM的发生。

LruCache特别适用于图片内存缓存这种有可能需要占用很多内存,但是只有最近使用的对象才有可能用到的场景。

1.2 LruCache 的使用

下面,我们用一个例子来演示一下LruCache的使用,让大家有一个初步的认识。

public class LruCacheSamples {

    private static final int MAX_SIZE = 50;

    public static void startRun() {
        LruCacheSample sample = new LruCacheSample();
        Log.d("LruCacheSample", "Start Put Object1, size=" + sample.size());
        sample.put("Object1", new Holder("Object1", 10));

        Log.d("LruCacheSample", "Start Put Object2, size=" + sample.size());
        sample.put("Object2", new Holder("Object2", 20));

        Log.d("LruCacheSample", "Start Put Object3, size=" + sample.size());
        sample.put("Object3", new Holder("Object3", 30));

        Log.d("LruCacheSample", "Start Put Object4, size=" + sample.size());
        sample.put("Object4", new Holder("Object4", 10));
    }

    static class LruCacheSample extends LruCache<String, Holder> {

        LruCacheSample() {
            super(MAX_SIZE);
        }

        @Override
        protected int sizeOf(String key, Holder value) {
            return value.getSize();
        }

        @Override
        protected void entryRemoved(boolean evicted, String key, Holder oldValue, Holder newValue) {
            if (oldValue != null) {
                Log.d("LruCacheSample", "remove=" + oldValue.getName());
            }
            if (newValue != null) {
                Log.d("LruCacheSample", "add=" + newValue.getName());
            }
        }
    }

    static class Holder {

        private String mName;
        private int mSize;

        Holder(String name, int size) {
            mName = name;
            mSize = size;
        }

        public String getName() {
            return mName;
        }

        public int getSize() {
            return mSize;
        }
    }
}
复制代码

运行结果为:

在放入 Object3之后,由于放入之前 LruCache的大小为 30,而 Object3的大小为 30,放入之后的大小为 60,超过了最先设定的最大值 50,因此会移除最先插入的 Object1,减去该元素的大小 10,最新的大小变为 50

二、源码解析

2.1 构造函数

首先看一下LruCache的构造函数:

    /**
     * @param maxSize for caches that do not override {@link #sizeOf}, this is
     *     the maximum number of entries in the cache. For all other caches,
     *     this is the maximum sum of the sizes of the entries in this cache.
     */
    public LruCache(int maxSize) {
        if (maxSize <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        }
        //最大的阈值。
        this.maxSize = maxSize;
        //用于存放缓存在内存中的对象
        this.map = new LinkedHashMap<K, V>(0, 0.75f, true);
    }
复制代码

当我们创建一个LruCache类时需要指定一个最大的阈值maxSize,而我们的对象会缓存在LinkedHashMap当中:

  • maxSize等于LinkedHashMap中每个元素的sizeOf(key, value)之和,默认情况下每个对象的大小为1,使用者可以通过重写sizeOf指定对应元素的大小。
  • LinkedHashMap是实现LRU算法的核心,它会根据对象的使用情况维护一个双向链表,其内部的header.after指向历史最悠久的元素,而header.before指向最年轻的元素,这一“年龄”的依据可以是访问的顺序,也可以是写入的顺序。

2.2 put 流程

接下来看一下与put相关的方法:

    /**
     * Caches {@code value} for {@code key}. The value is moved to the head of
     * the queue.
     *
     * @return the previous value mapped by {@code key}.
     */
    public final V put(K key, V value) {
        if (key == null || value == null) {
            throw new NullPointerException("key == null || value == null");
        }

        V previous;
        //同步代码块,因此是线程安全的。
        synchronized (this) {
            putCount++;
            //获得该对象的大小,由 LruCache 的使用者来决定,要求返回值大于等于 0,否则抛出异常。
            size += safeSizeOf(key, value);
            //调用的是 HashMap 的 put 方法,previous 是之前该 key 值存放的对象。
            previous = map.put(key, value);
            //如果已经存在,由于它现在被替换成了新的 value,所以需要减去这个大小。
            if (previous != null) {
                size -= safeSizeOf(key, previous);
            }
        }
        //通知使用者该对象被移除了。
        if (previous != null) {
            entryRemoved(false, key, previous, value);
        }
        //由于放入了新的对象,因此需要确保目前总的容量没有超过设定的阈值。
        trimToSize(maxSize);
        return previous;
    }

    private int safeSizeOf(K key, V value) {
        int result = sizeOf(key, value);
        if (result < 0) {
            throw new IllegalStateException("Negative size: " + key + "=" + value);
        }
        return result;
    }

    /**
     * Returns the size of the entry for {@code key} and {@code value} in
     * user-defined units.  The default implementation returns 1 so that size
     * is the number of entries and max size is the maximum number of entries.
     *
     * <p>An entry's size must not change while it is in the cache.
     */
    protected int sizeOf(K key, V value) {
        //默认情况下,每个对象的权重值为 1。
        return 1;
    }

    /**
     * Remove the eldest entries until the total of remaining entries is at or
     * below the requested size.
     *
     * @param maxSize the maximum size of the cache before returning. May be -1
     *            to evict even 0-sized elements.
     */
    public void trimToSize(int maxSize) {
        while (true) {
            K key;
            V value;
            synchronized (this) {
                if (size < 0 || (map.isEmpty() && size != 0)) {
                    throw new IllegalStateException(getClass().getName()
                            + ".sizeOf() is reporting inconsistent results!");
                }
                //这是一个 while 循环,因此将一直删除最悠久的结点,直到小于阈值。
                if (size <= maxSize) {
                    break;
                }
                //获得历史最悠久的结点。
                Map.Entry<K, V> toEvict = map.eldest();
                if (toEvict == null) {
                    break;
                }

                key = toEvict.getKey();
                value = toEvict.getValue();
                //从 map 中将它移除。
                map.remove(key);
                size -= safeSizeOf(key, value);
                evictionCount++;
            }
            //通知使用者该对象被移除了。
            entryRemoved(true, key, value, null);
        }
    }
复制代码

关于代码的解释都在注释中了,其核心的思想就是在每放入一个元素之后,通过sizeOf来获得这个元素的权重值,如果发现所有元素的权重值之和大于size,那么就通过trimToSize移除历史最悠久的元素,并通过entryRemoved回调给LruCache的实现者。

2.3 get 流程

    /**
     * Returns the value for {@code key} if it exists in the cache or can be
     * created by {@code #create}. If a value was returned, it is moved to the
     * head of the queue. This returns null if a value is not cached and cannot
     * be created.
     */
    public final V get(K key) {
        //与 HashMap 不同,LruCache 不允许 key 值为空。
        if (key == null) {
            throw new NullPointerException("key == null");
        }

        V mapValue;
        synchronized (this) {
            //首先在 map 中查找,如果找到了就直接返回。
            mapValue = map.get(key);
            if (mapValue != null) {
                hitCount++;
                return mapValue;
            }
            missCount++;
        }

        //如果在 map 中没有找到,get 方法不会直接返回 null,而是先回调 create 方法,让使用者有一个创建的机会。
        V createdValue = create(key);
        //如果使用者没有重写 create 方法,那么会返回 null。
        if (createdValue == null) {
            return null;
        }

        synchronized (this) {
            createCount++;
            //由于 create 的过程没有加入同步块,因此有可能在创建的过程中,使用者通过 put 方法在 map 相同的位置放入了一个对象,这个对象是 mapValue。
            mapValue = map.put(key, createdValue);
            //如果存在上面的情况,那么会抛弃掉 create 方法创建对象,重新放入已经存在于 map 中的对象。
            if (mapValue != null) {
                map.put(key, mapValue);
            } else {
                //增加总的权重大小。
                size += safeSizeOf(key, createdValue);
            }
        }
        //如果存在冲突的情况,那么要通知使用者这一变化,但是有大小并没有改变,所以不需要重新计算大小。
        if (mapValue != null) {
            entryRemoved(false, key, createdValue, mapValue);
            return mapValue;
        } else {
            //由于大小改变了,因此需要重新计算大小。
            trimToSize(maxSize);
            return createdValue;
        }
    }
复制代码

这里需要特别说明一下LruCacheHashMapget方法的区别:如果LinkedHashMap中不存在Key对应的Valueget方法并像HashMap一样直接返回,而是先 通过create方法尝试让使用者重新创建一个对象,如果创建成功,那么将会把这个对象放入到集合当中,并返回这个新创建的对象。

上面这种是单线程的情况,如果在多线程的情况下,由于create方法没有加入synchronized关键字,因此有可能 一个线程在create方法创建对象的过程中,另一个线程又通过put方法在Key对应的相同位置放入一个对象,在这种情况下,将会抛弃掉由create创建的对象,维持原有的状态。

2.4 LinkedHashMap

通过get/set方法,我们可以知道LruCache是通过trimToSize来保证它所维护的对象的权重之和不超过maxSize,最后我们再来分析一下LinkedHashMap,看下它是如何保证每次大小超过maxSize时,移除的都是历史最悠久的元素的。

LinkedHashMap继承于HashMap,它通过重写相关的方法在HashMap的基础上实现了双向链表的特性。

2.4.1 Entry 元素

LinkedHashMap重新定义了HashMap数组中的HashMapEntry,它的实现为LinkedHashMapEntry,除了原有的nextkeyvaluehash值以外,它还额外地保存了afterbefore两个指针,用来实现根据写入顺序或者读取顺序来排列的双向链表。

    private static class LinkedHashMapEntry<K,V> extends HashMapEntry<K,V> {

        LinkedHashMapEntry<K,V> before, after;

        LinkedHashMapEntry(int hash, K key, V value, HashMapEntry<K,V> next) {
            super(hash, key, value, next);
        }
        
        //删除链表结点。
        private void remove() {
            before.after = after;
            after.before = before;
        }
        
        //在 existingEntry 之前插入该结点。
        private void addBefore(LinkedHashMapEntry<K,V> existingEntry) {
            after  = existingEntry;
            before = existingEntry.before;
            before.after = this;
            after.before = this;
        }
        
        //如果是按访问顺序排列,那么将该结点插入到整个链表的头部。
        void recordAccess(HashMap<K,V> m) {
            LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
            if (lm.accessOrder) {
                lm.modCount++;
                remove();
                addBefore(lm.header);
            }
        }

        //从链表中移除该结点。
        void recordRemoval(HashMap<K,V> m) {
            remove();
        }
    }
复制代码

2.4.2 初始化

LinkedHashMap重写了init()方法,该方法会在其父类HashMap的构造函数中被调用,在init()方法中,会初始化一个空的LinkedHashMapEntry结点header,它的before指向最年轻的元素,而after指向历史最悠久的元素。

    void init() {
        header = new LinkedHashMapEntry<>(-1, null, null, null);
        header.before = header.after = header;
    }
复制代码

LinkedHashMap的构造函数中,可以传入accessOrder,如果accessOrdertrue,那么“历史最悠久”的元素表示的是访问时间距离当前最久的元素,即按照访问顺序排列;如果为false,那么表示最先插入的元素,即按照插入顺序排列,默认的值为false

2.4.3 元素写入

对于元素的写入,LinkedHashMap并没有重写put方法,而是重写了addEntry/createEntry方法,在创建结点的同时,更新它所维护的双向链表。

    void addEntry(int hash, K key, V value, int bucketIndex) {
        LinkedHashMapEntry<K,V> eldest = header.after;
        if (eldest != header) {
            boolean removeEldest;
            size++;
            try {
                removeEldest = removeEldestEntry(eldest);
            } finally {
                size--;
            }
            if (removeEldest) {
                removeEntryForKey(eldest.key);
            }
        }
        super.addEntry(hash, key, value, bucketIndex);
    }

    void createEntry(int hash, K key, V value, int bucketIndex) {
        HashMapEntry<K,V> old = table[bucketIndex];
        LinkedHashMapEntry<K,V> e = new LinkedHashMapEntry<>(hash, key, value, old);
        table[bucketIndex] = e;
        e.addBefore(header);
        size++;
    }
复制代码

2.4.4 元素读取

对于元素的读取,LinkedHashMap重写了get方法,它首先调用HashMapgetEntry方法找到结点,如果判断是需要根据访问的顺序来排列双向列表,那么就需要对链表进行更新,即调用我们在2.4.1中看到的recordAccess方法。

    public V get(Object key) {
        LinkedHashMapEntry<K,V> e = (LinkedHashMapEntry<K,V>)getEntry(key);
        if (e == null)
            return null;
        e.recordAccess(this);
        return e.value;
    }
复制代码

三、参考文献

深入 Java 集合学习系列:LinkedHashMap 的实现原理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值