问题
I get "bitmap size exceeds VM budget", eventually with my app. So I added all of these things to help alleviate the increasing memory footprint
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[32*1024];
options.inDither=false; //Disable Dithering mode
options.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
options.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
options.inPreferredConfig = Bitmap.Config.RGB_565;
Drawable pulled = BitmapDrawable.createFromResourceStream(null, null, conn.getInputStream(), "galleryImage", options);
I am also using weakhashmaps, recycling, System.gc()
and all this successfully PROLONGES the crash. where a device with a 32M heap initially would have only been able to handle a few images before crashing, now it can handle a dozen, but this doesn't fix the problem. The problem is the BitMap method is leaking memory and simply will not clear.
How do I fix this? The images are of size 256x357
回答1:
You should try to use drawable.setCallback(null);
when you don't need a drawable anymore, because even if you're using a WeakHashMap, they can still be attached to the context through their callback attribute.
See this
回答2:
If you want to really make sure those Bitmaps
are freed, you have to call recycle()
on those Bitmaps
that are no longer needed. If you need all of them, then recycle least recently used ones (and re-load them, again when needed).
回答3:
I was dealing with ImageView and i solved the memory problem using imageView.clearAnimation(); before to assign the new bitmap and now i have no memory error.
来源:https://stackoverflow.com/questions/8460410/android-bitmap-freeing-memory-none-of-this-works