2

There's an activity in my app whose showing around 1000 very small sized bitmaps (Around 20kb each bitmap). After it loads some of the bitmaps, there's an OutOfMemoryException.

I was first reading about SoftReference and it looked like it will solve my problem about the OOM exceptions. But then, I read that it won't cache my bitmaps and will free them "too soon", so it will have to decode the bitmap again and "waste" time.So, I implemented the LruCache.

How can I make sure that I will not get OOM exception when implementing my LruCache?

Maybe I should just use the SoftReference, because my main target is to avoid OOM

Or, this might be my solution? LruSoftCache

4

1 回答 1

2

在实现时LruCache,你应该指定缓存大小,并告诉它如何计算每个对象的大小(在这种情况下,对象是位图)。

您可以使用以下示例:

// uses 1/8th of the memory for the cache
final int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 8L);
LruCache bitmapCache = new LruCache(cacheSize) {
   protected int sizeOf(String key, Bitmap value) {
       return value.getByteCount();
}}
于 2013-10-15T17:51:26.060 回答