Take the 2-minute tour ×

When displaying a gallery with lots of large bitmaps (fullscreen, wvga), I quite frequently get out of memory issues. I assume this is related to bitmaps not being recycled. When/how can I force bitmaps to be recycled?

I also noticed that in the getView method of simpleCursorAdapter, convertView is always null. I assume this means that the old view is not recycled? Even when scrolling back and forth, a new view is created each time. However, scrolling back and forth does not cause out of memory issues, that only happens when the total number of images is large enough.

When using the same adapter with a ListView, views are recycled, so it seems the problem is with the Gallery.

What can I do to force views and/or bitmaps to be recycled? What else can I do manage memory without reducing gallery size and bitmap quality.

share|improve this question
Have you tried the various bitmap options like purgeable - are you using pre-scaled or high quality images for the gallery? Have you seen SoftReference / WeakReference for memory management? –  Sebastian Roth Dec 15 '10 at 13:56
add comment

2 Answers

You can use recycle() if you are sure tou won't use a bitmap anymore, for example in any operation with a temporary bitmap. But...You can use BitmapFactory.Options, for ex:

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

Bitmap bitmapTemp = BitmapFactory.decodeResource(getResources(), R.drawable.intro, bitmapOptions);

What it does? the option inPurgeable will asume that once the system needs memory and your bitmap it's no more useful, system will recycle this memory allocation by itself. If for some reason you cannot load an image with the BitmapFactory then you can use Recycle() and it's recommendable to call de garbage collector then with system.gc(), this is usefull if you're developing for example a game, because when garbage collector it's activated when you system it's low on memory, it will consume some valious milliseconds. You should be very careful with bitmaps, they're not stored on the "normal" memory used by your app, it's stored on a "general" memory used by all your apps, unless you are using Android 3.0. If you get a memory leak and can't find it looking at the heap... you should take a look at your code loading and using bitmaps. Hope it helps.

share|improve this answer
add comment

You never need to recyle a Bitmap. However, you can do it, and it will help when you have OutOfMemory errors. But don't it until the bitmap is not needed any more, because (from the javadoc)

it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap.

In your case, I assume you should:

share|improve this answer
right, you don't need to, but your application will crash if you don't. how exactly do you define "need"? –  Jeffrey Blattman Apr 22 '11 at 21:54
Google definition of "need" is different. You have to recycle bitmaps in pre-HC. –  Raz Mar 14 '12 at 13:50
add comment

Your Answer


