TooManyBitmapsException cause no display
how to clear memory cache or how to fix it?
Any follow up? It appears frequently.
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 */);
}
However, step back a bit. TooManyBitmapsException usually means you have too many bitmaps open. What is your application doing?
What are the parent views/layouts of the DraweeViews?
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
do you means trimmable clear the cache should app on background?but i think it's bad when user use it
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,
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?
Are you using Drawee, or are you just using the image pipeline directly?
use SimpleDraweeView
http://frescolib.org/docs/caching.html#clearing-the-cache
@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?
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 you saved my day. Setting MAX_BITMAP_COUNT 380 in pre Lollipop devices fixed my problem