LruCache详解之 Android 内存优化

概念:

LruCache 什么是LruCache? LruCache实现原理是什么?

这两个问题其实可以作为一个问题来回答,知道了什么是 LruCache,就只然而然的知道 LruCache 的实现原理;Lru的全称是Least Recently Used ,近期最少使用的!所以我们可以推断出 LruCache 的实现原理:把近期最少使用的数据从缓存中移除,保留使用最频繁的数据,那具体代码要怎么实现呢,我们进入到源码中看看。

LruCache源码分析public class LruCache<K, V> {final LinkedHashMap<K, V> map;size;maxSize;putCount;createCount;evictionCount;hitCount;missCount;(int maxSize) {if (maxSize <= 0) {throw new IllegalArgumentException(“maxSize <= 0”);}this.maxSize = maxSize;this.map = new LinkedHashMap<K, V>(0, 0.75f, true);}(int maxSize) {if (maxSize <= 0) {throw new IllegalArgumentException(“maxSize <= 0”);}synchronized (this) {this.maxSize = maxSize;}trimToSize(maxSize);}//通过 key 获取缓存值public final V get(K key) {if (key == null) {throw new NullPointerException(“key == null”);}V mapValue;synchronized (this) {mapValue = map.get(key);if (mapValue != null) {hitCount++;return mapValue;}missCount++;}//如果没有,用户可以去创建V createdValue = create(key);if (createdValue == null) {return null;}synchronized (this) {createCount++;mapValue = map.put(key, createdValue);if (mapValue != null) {// There was a conflict so undo that last putmap.put(key, mapValue);} else {//缓存的大小改变size += safeSizeOf(key, createdValue);}}//这里没有移除,只是改变了位置if (mapValue != null) {entryRemoved(false, key, createdValue, mapValue);return mapValue;} else {//判断缓存是否越界trimToSize(maxSize);return createdValue;}}//添加缓存,跟上面这个方法的 create 之后的代码一样的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++;size += safeSizeOf(key, value);previous = map.put(key, value);if (previous != null) {size -= safeSizeOf(key, previous);}}if (previous != null) {entryRemoved(false, key, previous, value);}trimToSize(maxSize);return previous;}(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!”);}//如果没有,则返回if (size <= maxSize) {break;}//以下代码表示已经超出了最大范围Map.Entry<K, V> toEvict = null;for (Map.Entry<K, V> entry : map.entrySet()) {toEvict = entry;}if (toEvict == null) {break;}//移除最后一个,也就是最少使用的缓存key = toEvict.getKey();value = toEvict.getValue();map.remove(key);size -= safeSizeOf(key, value);evictionCount++;}entryRemoved(true, key, value, null);}}//手动移除,用户调用public final V remove(K key) {if (key == null) {throw new NullPointerException(“key == null”);}V previous;synchronized (this) {previous = map.remove(key);if (previous != null) {size -= safeSizeOf(key, previous);}}if (previous != null) {entryRemoved(false, key, previous, null);}return previous;}(boolean evicted, K key, V oldValue, V newValue) {}protected V create(K key) {return null;}(K key, V value) {int result = sizeOf(key, value);if (result < 0) {throw new IllegalStateException(“Negative size: ” + key + “=” + value);}return result;}(K key, V value) {return 1;}() {trimToSize(-1); // -1 will evict 0-sized elements}public synchronized final int size() {return size;}public synchronized final int maxSize() {return maxSize;}public synchronized final int hitCount() {return hitCount;}public synchronized final int missCount() {return missCount;}public synchronized final int createCount() {return createCount;}public synchronized final int putCount() {return putCount;}public synchronized final int evictionCount() {return evictionCount;}public synchronized final Map<K, V> snapshot() {return new LinkedHashMap<K, V>(map);}}LruCache 使用

先来看两张内存使用的图

图-1

图-2这一次是一个告别,或者一个永远的告别,

LruCache详解之 Android 内存优化

相关文章:

你感兴趣的文章:

标签云: