Take the 2-minute tour ×

How to clear the cached image from memory programatically in Android?

I have a ListView with icons when I scroll its reloads the image. So its produce the OutofMemoryError. I want clear the cache while gets this exception. how to do that? any help?


i am just using this code on my program to loadimage: http://ballardhack.wordpress.com/2010/04/10/loading-images-over-http-on-a-separate-thread-on-android/

share|improve this question
Can you post code? Let's look at why you're getting a memory error. –  Dylan McClung May 28 '10 at 14:50

3 Answers 3

Yup... known "problem", let say this is the behaviour of the ListView.

How to fix it:

  1. Watch the first 15min of the video as suggested by @HXCaine, which explains the ViewHolder.

  2. If I'm not mistaken your example should set the default image when a bitmap is null! In the example you do not provide this to a view and so it gets cached. Shur this should be handled by the framework but it is not :(.

Example code:

public class DebtAdapter extends BaseAdapter {

            ViewHolder holder;
            Bitmap bitmap;

            if(convertView == null)  
                convertView = inflater.inflate(viewResourceId, null);

                holder = new ViewHolder();
                holder.photo = (ImageView) convertView.findViewById(R.id.photo);

                holder = (ViewHolder) convertView.getTag();
            bitmap = item.getContact().getPhoto();
            if(bitmap != null)
            return convertView;

I hope it helps.

share|improve this answer
Note alos I used an imaginary item variable that returns a bitmap object. –  xpepermint Oct 18 '10 at 12:18

Are you re-using the bitmap objects in the ListView?

Romain Guy talked about how important this is for memory and smooth performance in his Android talk on layouts and views at Google I/O last year.

Essentially, you should have a certain number of bitmap objects (he used 8) and every time you load the next image as you scroll, it should go into the object of the one that just disappeared.

You might think caching the images is faster, but it causes memory problems and garbage collecting issues which inevitably causes lag.

share|improve this answer
Not sure what you mean by re-using bitmap object..do you mean re-using the views of the list items (ie. using convert-view)?. Because I cant see any mention of re-using the actual bitmap object to improve preform in that talk :S –  Kman Sep 22 '10 at 3:49
Maybe I was mistaken, but the idea is that you recycle objects to prevent the GC repeatedly making collections and reducing performance –  HXCaine Sep 27 '10 at 11:31

You can free up some memory by calling the recycle method if you're using Bitmap. However, I'm not really sure if this will solve your problem.

share|improve this answer
Thanks for reply. Not working. –  Praveen May 28 '10 at 14:33
How would you call .recycle() on a Bitmap that is cleared by SoftReference, I think you can't ? –  2cupsOfTech Jan 2 '11 at 19:22

Your Answer


