OutOfMemoryError - Breaking the heap size with bitmaps

69 views Asked by At

I'm getting an OutOfMemoryError exception thrown after a few loads of my apps main screen. The app loads data and a background image about a location. There are several options to choose from, and after 5-6 different location selections, the heap exceeds 192 MB and I get my error. I believe the issues is down to my using bitmaps poorly.

My code is quite long and unclear, so I'll give an example and hopefully someone can help me, as I think I'm failing to grasp the basics regarding image memory management (I have read documentation and searched extensively, I'm looking to ideally actually converse with someone about this).


I have an if with 5 conditions, each returns a different image. I convert the image to a bitmap and set it as the new drawable source for my main layout.

Then I do this again, same function but for a different location, with a different image.

After a few times doing this, all these images being loaded seem to remain in the memory, and clog it up, leading to an error. Trying to keep it very simple, what steps should I be implementing between loading these different images into my layouts background to prevent this issue?

1

There are 1 answers

5
Denis Itskovich On

You have to recycle bitmaps, you are not using anymore. Make sure you are holding very limited number of Bitmap instances at the same time, and each time you finishing using a bitmap call its "recycle()" method.

You can find more information here: Managing Bitmap Memory & Caching Bitmaps