0

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

CQM
  • 42,592
  • 75
  • 224
  • 366

3 Answers3

5

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).

inazaruk
  • 74,247
  • 24
  • 188
  • 156
3

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

Stecya
  • 22,896
  • 10
  • 72
  • 102
JeromePApp
  • 209
  • 1
  • 6
0

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.

Gaucho
  • 1,328
  • 1
  • 17
  • 32