首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android图像缓存内存异步问题

Android图像缓存内存异步问题
EN

Stack Overflow用户
提问于 2013-10-11 16:49:19
回答 1查看 439关注 0票数 0

我正在使用图像加载器类来加载和捕获图像,但我遇到了内存大小的问题以及它引发的logcat消息,如下所示,直到我的应用程序冻结执行,我不明白为什么会发生这种情况?

代码语言:javascript
复制
10-11 14:11:22.735: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.735: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.745: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.755: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.755: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.765: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.775: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.775: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.784: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.795: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.795: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.806: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.814: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.814: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.824: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.824: I/MemoryCache(859): MemoryCache will use up to 125.0MB
10-11 14:11:22.834: I/MemoryCache(859): MemoryCache will use up to 125.0MB

这是我的内存捕获类

代码语言:javascript
复制
 public class MemoryCache {

    private static final String TAG = "MemoryCache";
    private Map<String, Bitmap> cache=Collections.synchronizedMap(
            new LinkedHashMap<String, Bitmap>(10,1.5f,true));//Last argument true for LRU ordering
    private long size=0;//current allocated size
    private long limit=1000000;//max memory in bytes

    public MemoryCache(){

        //use 25% of available heap size
        setLimit(Runtime.getRuntime().maxMemory()/4);
        //setLimit(Runtime.getRuntime().maxMemory()/4);
    }

    public void setLimit(long new_limit){
        limit=new_limit;

        Log.i(TAG, "MemoryCache will use up to "+limit/1024./1024.+"MB");
    }

    public Bitmap get(String id){
        try{
            if(!cache.containsKey(id))
                return null;
            //NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78 

            return cache.get(id);
        }catch(NullPointerException ex){
            ex.printStackTrace();
            return null;
        }
    }

    public void put(String id, Bitmap bitmap){
        try{
            if(cache.containsKey(id))
                size-=getSizeInBytes(cache.get(id));
            cache.put(id, bitmap);
            size+=getSizeInBytes(bitmap);
            checkSize();
        }catch(Throwable th){
            th.printStackTrace();
        }
    }

    private void checkSize() {
        Log.i(TAG, "cache size="+size+" length="+cache.size());
        if(size>limit){
            Iterator<Entry<String, Bitmap>> iter=cache.entrySet().iterator();//least recently accessed item will be the first one iterated  
            while(iter.hasNext()){
                Entry<String, Bitmap> entry=iter.next();
                size-=getSizeInBytes(entry.getValue());
                iter.remove();
                if(size<=limit)
                    break;
            }
            Log.i(TAG, "Clean cache. New size "+cache.size());
        }
    }

    public void clear() {
        try{
            //NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78 
            cache.clear();
            size=0;
        }catch(NullPointerException ex){
            ex.printStackTrace();
        }
    }

    long getSizeInBytes(Bitmap bitmap) {
        if(bitmap==null)
            return 0;
        return bitmap.getRowBytes() * bitmap.getHeight();
    }
}

我正在使用异步任务机制在列表视图中调用此图像,您的帮助将对我非常有用,谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-10-11 17:22:10

为什么Log.i(TAG, "MemoryCache will use up to "+limit/1024./1024.+"MB");会被调用这么多次?请检查您是否一直在创建MemoryCache实例,还是多次在某个地方调用setLimit

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19313963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档