Saturday, September 24, 2011

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

I recently got this error in a crash report from the android market for my app.

The problem was that I was trying to load many bitmaps into a gridview (thus causing the app to run out of memory). The problem became quite noticeable at around 20 bitmaps and over.

I was trying to fix the problem with all kinds of silly band-aid solutions (eg calling the garbage collector in getView of the adapter, yikes!)

I managed to identify the root cause of the problem: I wasn't scaling the images in the BitmapFactory.

I was doing this:

BitmapFactory factory = new BitmapFactory();
Bitmap img = factory.decodeFile(path);


when I should have been doing this:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
options.outHeight = HEIGHT;
options.outWidth = WIDTH;

BitmapFactory factory = new BitmapFactory();

Bitmap img = factory.decodeFile(path,options);

After 30 seconds of vigorous scrolling of the gridview, I think I have solved it. Or maybe not, feel free to try out my app and let me know if it crashes again:

https://market.android.com/details?id=remember.me


I hope that helps.

No comments: