public class BitmapCache {
? ? static private BitmapCache cache;
? ? /** 用于Chche内容的存储 */
? ? private Hashtable<Integer, MySoftRef> hashRefs;
? ? /** 垃圾Reference的队列(所引用的对象已经被回收,则将该引用存入队列中) */
? ? private ReferenceQueue<Bitmap> q;
? ? /**
? ???* 继承SoftReference,使得每一个实例都具有可识别的标识。
? ?? ? */
? ? private class MySoftRef extends SoftReference<Bitmap> {
? ?? ??? private Integer _key = 0;
? ?? ??? public MySoftRef(Bitmap bmp, ReferenceQueue<Bitmap> q, int key) {
? ?? ?? ?? ? super (bmp, q);
? ?? ?? ?? ?_key = key;
? ?? ???}
? ? }
? ? private BitmapCache() {
? ?? ???hashRefs = new Hashtable<Integer, MySoftRef>();
? ?? ???q = new ReferenceQueue<Bitmap>();
? ? }
? ? /**
? ???* 取得缓存器实例
? ?? ? */
? ? public static BitmapCache getInstance() {
? ?? ??? if (cache == null ) {
? ?? ?? ?? ?cache = new BitmapCache();
? ?? ???}
? ?? ??? return cache;
? ? }
? ? /**
? ???* 以软引用的方式对一个Bitmap对象的实例进行引用并保存该引用
? ?? ? */
? ? private void addCacheBitmap(Bitmap bmp, Integer key) {
? ?? ???cleanCache(); // 清除垃圾引用
? ?? ?? ?MySoftRef ref = new MySoftRef(bmp, q, key);
? ?? ???hashRefs.put(key, ref);
? ? }
? ? /**
? ???* 依据所指定的drawable下的图片资源ID号(可以根据自己的需要从网络或本地path下获取),重新获取相应Bitmap对象的实例
? ??? */
? ? public Bitmap getBitmap( int resId, Context context) {
? ?? ???Bitmap bmp = null ;
? ?? ??? // 缓存中是否有该Bitmap实例的软引用,如果有,从软引用中取得。
? ?? ?? ? if (hashRefs.containsKey(resId)) {
? ?? ?? ?? ?MySoftRef ref = (MySoftRef) hashRefs.get(resId);
? ?? ?? ?? ?bmp = (Bitmap) ref.get();
? ?? ???}
? ?? ??? // 如果没有软引用,或者从软引用中得到的实例是null,重新构建一个实例,
? ?? ?? ? // 并保存对这个新建实例的软引用
? ?? ?? ? if (bmp == null ) {
? ?? ?? ?? ? // 传说decodeStream直接调用JNI>>nativeDecodeAsset()来完成decode,
? ?? ?? ?? ??? // 无需再使用java层的createBitmap,从而节省了java层的空间。
? ?? ?? ?? ???bmp = BitmapFactory.decodeStream(context.getResources()
? ?? ?? ?? ?? ?? ???.openRawResource(resId));
? ?? ?? ?? ? this .addCacheBitmap(bmp, resId);
? ?? ???}
? ?? ??? return bmp;
? ? }
? ? private void cleanCache() {
? ?? ???MySoftRef ref = null ;
? ?? ??? while ((ref = (MySoftRef) q.poll()) != null ) {
? ?? ?? ?? ?hashRefs.remove(ref._key);
? ?? ???}
? ? }
? ? /**
? ???* 清除Cache内的全部内容
? ??? */
? ? public void clearCache() {
? ?? ???cleanCache();
? ?? ???hashRefs.clear();
? ?? ???System.gc();
? ?? ???System.runFinalization();
? ? }
}
*****************************************************
*****************************************************
一般Java虚拟机要求支持verbosegc选项,输出详细的垃圾收集调试信息。dalvik虚拟机很安静的接受verbosegc选项,然后什么都不做。dalvik虚拟机使用自己的一套LOG机制来输出调试信息。
如果在Linux下运行adb logcat命令,可以看到如下的输出:
D/dalvikvm(??745): GC_CONCURRENT
freed 199K, 53% free 3023K/6343K,external 0K/0K, paused 2ms+2ms
其中D/dalvikvm表示由dalvikvm输出的调试信息,括号后的数字代表dalvikvm所在进程的pid。
GC_CONCURRENT表示触发垃圾收集的原因,有以下几种:
GC_MALLOC, 内存分配失败时触发
GC_CONCURRENT,当分配的对象大小超过384K时触发
GC_EXPLICIT,对垃圾收集的显式调用(System.gc)
GC_EXTERNAL_ALLOC,外部内存分配失败时触发
freed 199K表示本次垃圾收集释放了199K的内存,
53% free 3023K/6343K,其中6343K表示当前内存总量,3023K表示可用内存,53%表示可用内存占总内存的比例。
external 0K/0K,表示可用外部内存/外部内存总量
paused
2ms+2ms,第一个时间值表示markrootset的时间,第二个时间值表示第二次mark的时间。如果触发原因不是GC_CONCURRENT,这一行为单个时间值,表示垃圾收集的耗时时间。
详细解决方案
[课程] 【转】Android 通过软引用实现图片缓存,防止内存溢出 [复制链接]
热度:358 发布时间:2016-05-01 13:38:17.0
相关解决方案
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android 大量数据按周分组,该如何解决
- android RadioButton如何设置默认选中
- ksoap2-android-这个包,连接webService怎么设置超时
- android 怎么重新设置锚点
- android UI界面设计解决方案
- android 图片对象获取的有关问题
- android 怎么调用淘宝支付宝接口
- Android 沿袭InputMethodService自定义输入法
- android 关于服务连接的疑义
- android 两个activity如何通信
- android 怎么实现对view的放大和缩小
- android 教程解决方法
- android ID,该如何处理
- 准备复习2-3个月,看java+android,请问有经验者,怎么看效果最好》
- android UI线程与AsyncTask的有关问题
- android(java)中的java.net能不能和c#的system.net.sockets进行tcp通信,该如何解决
- android ListView 中的onItemClick Intent 没法跳转
- android(java) 中文乱码的有关问题
- c#c++,android,ios(iphone),php,java视屏课程 散分
- android Post文件到ASP.NET的有关问题,能收到参数收不到文件
- RIM 替 Android 开发者提供免费的 PlayBook!2月13日前
- android 动态设立控件高度
- Android test project 编译方法
- android -相机使用教程(1)解决方法