strange behaviour caching bitmaps in a lrucache

I am using a LRUCache to cache different objects and to avoid outofmemory problems. This works fine for all kind of javaobjects except for bitmaps. With bitmaps I still get outofmemory when decoding a file (nativeDecodeAsset) after a while. I can follow the memory consumption reaching the max.

I simply do the following:

public showbitmap(Canvas canvas,String BitmapID){

    Bitmap bmtemp=(Bitmap) lrucache.get(BitmapID);

    if(bmtemp==null){
        bmtemp=getBitmapfromFile(BitmapID);
        lrucache.put(BitmapID,bmtemp);

    }

    if(bmtemp!=null){
        canvas.drawBitmap(bmtemp, ..., ...);
    }

}

I do not use the bitmaps outside of my canvas. I thought the LRUCache should free it. Any hints ? I read alot on this in the forum, but still don't know howto avoid this. I guess it has to do with memory fragmentation ?

I enhanced my LRUCache with entryRemoved and I see when debugging that Bitmap.recycle is called.

@Override
                    protected void entryRemoved(boolean evicted, String key, Object oldobject, Object newobject) {
                       if(key.startsWith("_img_")){
                           ((Bitmap)oldobject).recycle();
                           Log.v("Debug recycle", "bitmap recycled !");
                       }
                    }

new Infos: I recognized that my total memory (Runtime.getRuntime().totalMemory()) is never reduced even if I manually clean the LRUCache with evictAll(). When total memory reaches nearly max Memory (Runtime.getRuntime().maxMemory()), then my OutofMemory Error comes.

I do not understand this. Why can't my app use the free space to decode the image (nativeDecodeAsset) when LRUCache is completely cleared ?

链接地址: http://www.djcxy.com/p/93066.html

上一篇: 清楚地解释OutOfMemoryError消息

下一篇: 奇怪的行为缓存lrucache中的位图