fresco icon indicating copy to clipboard operation
fresco copied to clipboard

TooManyBitmapsException cause no display

Open lindow2009 opened this issue 11 years ago • 14 comments

how to clear memory cache or how to fix it?

lindow2009 avatar Apr 29 '15 02:04 lindow2009

Any follow up? It appears frequently.

weilii avatar Apr 30 '15 09:04 weilii

The general solution to this is to make use of a MemoryTrimmableRegistry:

class MemoryManager implements MemoryTrimmableRegistry {
  List<MemoryTrimmable> trimmables;
  void registerMemoryTrimmable(MemoryTrimmable trimmable) {
    trimmables.add(trimmable);
  }

 /// some code that gets called when memory is low, or when you get this exception
 for (MemoryTrimmable trimmable : trimmables) {
    trimmable.trim(/* choose appropriate type here; OnAppBackgrounded will clear the cache */);
}

tyronen avatar Apr 30 '15 15:04 tyronen

However, step back a bit. TooManyBitmapsException usually means you have too many bitmaps open. What is your application doing?

tyronen avatar Apr 30 '15 15:04 tyronen

What are the parent views/layouts of the DraweeViews?

plamenko avatar Apr 30 '15 23:04 plamenko

just like pinterest,many images in one activity,so when activity jump,it may cause TooManyBitmapsException Especially in the mobile phone memory is too small. parent views/layouts of the DraweeViews is normal LinearLayout and RelativeLayout

lindow2009 avatar May 04 '15 02:05 lindow2009

do you means trimmable clear the cache should app on background?but i think it's bad when user use it

lindow2009 avatar May 04 '15 03:05 lindow2009

I got this method in BitmapCounter:

public synchronized boolean increase(Bitmap bitmap) {
    final int bitmapSize = getBitmapSize(bitmap);
    if (mCount >= mMaxCount || mSize + bitmapSize > mMaxSize) {
      return false;
    }
    mCount++;
    mSize += bitmapSize;
    return true;
  }

if return false, you will got a TooManyBitmapsExcetion. value of mMaxCount is 384, value of mMaxSize is:

private static int getMaxSizeHardCap() {
    final int maxMemory = (int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE);
    if (maxMemory > 16 * MB) {
      return maxMemory / 4 * 3;
    } else {
      return maxMemory / 2;
    }
  }

so, maybe your bitmap is too big,

xianfeng-zhu avatar May 05 '15 06:05 xianfeng-zhu

Actually, single image storage size is no more than 200k(#214), the bitmap is not big. It seems the cache size is overload when too many images between app's activity changing and device memory is low. So is there any method to clear the cache used by not showing views?

weilii avatar May 06 '15 03:05 weilii

Are you using Drawee, or are you just using the image pipeline directly?

IanChilds avatar May 11 '15 14:05 IanChilds

use SimpleDraweeView

lindow2009 avatar May 13 '15 02:05 lindow2009

http://frescolib.org/docs/caching.html#clearing-the-cache

tyronen avatar Jul 20 '15 21:07 tyronen

@tyronen I tried to use imagePipeline.clearCaches(), it doesn't work. Still got TooManyBitmapsException failure. I checked the code and didn't see any code decrease the mCount and mSize in com.facebook.imagepipeline.memory.BitmapCounter. Is there anything missed?

narychen avatar Jul 11 '16 07:07 narychen

you've created 384 bitmaps @see com.facebook.imagepipeline.memory.BitmapCounterProvider.MAX_BITMAP_COUNT so in less than LOLLIPOP of the phone, the limited number of bitmaps in the Ashmem can not be more than 384

    public MemoryCacheParams get() {
        int maxSize = (int) getMaxCacheSize();
        /**
         * fix fresco TooManyBitmapsException
         * @see com.facebook.imagepipeline.memory.BitmapCounterProvider.MAX_BITMAP_COUNT
         */
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return new MemoryCacheParams(maxSize, 380, maxSize, Integer.MAX_VALUE, Integer.MAX_VALUE);
        } else {
            return new MemoryCacheParams(maxSize, Integer.MAX_VALUE, maxSize, Integer.MAX_VALUE, Integer.MAX_VALUE);
        }
    }

tgithubc avatar Feb 09 '17 03:02 tgithubc

tgithubc you saved my day. Setting MAX_BITMAP_COUNT 380 in pre Lollipop devices fixed my problem

hovterteryan avatar May 24 '17 06:05 hovterteryan