Android LruCache究竟是什么
源码: /frameworks/base/core/java/android/util/LruCache.java
文件开篇注释如下:
A cache that holds strong references to a limited number of values. Each time a value is accessed, it is moved to the head of a queue. When a value is added to a full cache, the value at the end of that queue is evicted and may become eligible for garbage collection.
含义是:
一个只对有限个value保持强引用的Cache.
每次访问一个value, 它都会被移到队头.
当一个Cache满了之后, 如果再向它添加元素, 队尾的元素将会被回收.
<p>If your cached values hold resources that need to be explicitly released, override {@link #entryRemoved}.
如果有一个元素需要被显式的释放, 重写entryRemoved方法
protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {}
<p>If a cache miss should be computed on demand for the corresponding keys,override {@link #create}.
This simplifies the calling code, allowing it to assume a value will always be returned, even when there's a cache miss.
对这段理解的一直不是很好, 读完源码以后再说
protected V create(K key) {
return null;
}
<p>This class does not allow null to be used as a key or value.
A return value of null from {@link #get}, {@link #put} or {@link #remove} is unambiguous: the key was not in the cache.
这个类不允许Key 和 Value 是 null. 如果 get, put, remove中出现了null, 那么将会返回 该Cache中不存在这个key
关键代码:
public class LruCache<K, V> {
private final LinkedHashMap<K, V> map;
/** Size of this cache in units. Not necessarily the number of elements. */
private int size;
private int maxSize;
private int putCount;
private int createCount;
private int evictionCount;
private int hitCount;
private int missCount;
/**
* @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);
}
}
使用 LinkedHashMap来管理整个Cache
使用 LruCache 必须重写 sizeOf 方法, sizeOf方法用来计算 value的大小
protected int sizeOf(K key, V value) {
return 1;
}
使用方法
// 继承LruCache时,必须要复写sizeof方法,用于计算每个条目的大小
// cacheMemory 表示可用缓存的大小, value 也不能超出 cacheMemory
mLruCache = new LruCache<String, Bitmap>(cacheMemory) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
Android LruCache究竟是什么的更多相关文章
- Android LRUCache简介
LRU Cache数据结构的介绍可以参考前面的http://www.cnblogs.com/XP-Lee/p/3441555.html. 本文以Android LRUCache来做一个简单的介绍.我们 ...
- Android LruCache(Picasso内存缓存)
Cache保存一个强引用来限制内容数量,每当Item被访问的时候,此Item就会移动到队列的头部,当cache已满的时候加入新的item时,在队列尾部的item会被回收. 如果你cache的某个值需要 ...
- 【转】Android LruCache源码介绍
本文来源:转载自: http://blog.csdn.net/linghu_java/article/details/8574102 package android.util; import java ...
- Android LruCache技术原理
概述 记得在很早之前,我有写过一篇文章Android高效加载大图.多图解决方案,有效避免程序OOM,这篇文章是翻译自Android Doc的,其中防止多图OOM的核心解决思路就是使用LruCache技 ...
- Android LRUCache
package android.util; import java.util.LinkedHashMap; import java.util.Map; /** * A cache that holds ...
- Android LruCache 压缩图片 有效避免程序OOM
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9316683 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工, ...
- Android——LruCache源码解析
以下针对 Android API 26 版本的源码进行分析. 在了解LruCache之前,最好对LinkedHashMap有初步的了解,LruCache的实现主要借助LinkedHashMap.Lin ...
- Android LruCache源码简介
package android.util; import java.util.LinkedHashMap; import java.util.Map; /** * A cache that holds ...
- Android开发究竟用什么工具,Eclipse||AS
所谓公欲善其事必先利器,那就让我们来看一下android的开发工具吧,安卓的开发工具有Eclipse和Android Studio,另外还有IntelliJ IDEA,可能很多人并不知道. 首先看一下 ...
随机推荐
- C++如何用system命令获取文件夹下所有文件名
http://www.cplusplus.com/reference/cstdlib/system/ http://bbs.csdn.net/topics/30068943 #include < ...
- 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...
- JS类型(1)_JS学习笔记(2016.10.02)
js类型 js中的数据类型有undefined,boolean,number,string,null,object等6种,前5种为原始类型(基本类型),基本类型的访问是按值访问的,就是说你可以操作保存 ...
- JavaScript中的各种宽高以及位置总结
JavaScript中的各种宽高以及位置总结 在javascript中操作dom节点让其运动的时候,常常会涉及到各种宽高以及位置坐标等概念,如果不能很好地理解这些属性所代表的意义,就不能理解js的运动 ...
- Windows下性能最好的I/O模型——完成端口
I/O模型--完成端口 设计目的: 常见的网络通信分为两种:同步和异步. 在同步通信中,每一次接受数据都会导致主线程的挂起,从而阻塞住了其他操作.为了解决这一问题,我们通常会采取同步通信+多线程的策略 ...
- OQL对象查询语言
在用mat工具分析内存使用情况查询OutOfMemory原因时,OQL会有很大帮助,所以先在这里总结一下. 基本语法: select <javascript expression to sele ...
- 使用VS建立Web网站及IIS
一:网站运行的一般原理: 1.所有WEB程序运行都需要一个东西:Web服务器软件. 2. 网站分为动态页面和静态页面 3. web服务器软件的作用:接收请求,响应请求. 被动——无请求则无响应. Ja ...
- 快递查询api(多接口方案)
/** 本环境使用php+smarty,结合两种快递api调取快递数据 * 说明,先快递鸟调取数据,失败后再调取快递网的数据* 快递鸟 http://www.kdniao.com 快递网 http:/ ...
- Quartus II 11.0 开始使用
一.Altera Quartus II 11.0套件介绍 所谓巧妇难为无米之炊,再强的软硬件功底,再多的思维创造力,没有软件的平台,也只是徒劳.因此,一切创造的平台——Quartus II 软件安装, ...
- 预算oracle
select * from ( )) CODE_VERSION from ( SELECT tb_cube_fc05.pk_entity pk_org,/*主体pk*/ org_orgs.code o ...