user367785
user367785

Reputation:

ImageAdapter and ListView (java.lang.OutOfMemoryError: bitmap size exceeds VM budget)

I'm using ListView to display images which I provide through an ImageAdapter class. it works great on my device (and on many other devices which I tested it on), but somehow when I'm using the emulator and I'm long-pressing the up/down button - I'm getting an outOfMemory error after 10-15 seconds.

I tried clearing cache, canceling cache, etc. - nothing helped.

I know this crash is pretty rare (i couldn't reproduced it on any "real" device), but I can see on DDMS that "GC freed" are getting bigger during that long press and I can't find a way to clear them.

Any help will be appreciated, Tnx.

Upvotes: 1

Views: 4259

Answers (3)

BajaBob
BajaBob

Reputation: 2853

I just ran into this issue a couple minutes ago. I solved it by doing a better job at managing my listview adapter. I thought it was an issue with the hundreds of 50x50px images I was using, turns out I was trying to inflate my custom view each time the row was being shown. Simply by testing to see if the row had been inflated I eliminated this error, and I am using hundreds of bitmaps. This is actually for a Spinner, but the base adapter works all the same for a ListView. This simple fix also greatly improved the performance of the adapter.

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {

    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.spinner_row, null);
    }
...

Upvotes: 1

emmby
emmby

Reputation: 100464

I had the same problem in my ListView. I was able to reduce the memory footprint of my downloads by scaling my images in place and using inPurgeable as Fedor describes. See the code here: How do I scale a streaming bitmap in-place without reading the whole image first?

Upvotes: 0

Fedor
Fedor

Reputation: 43412

You should decode with inSampleSize option to reduce memory consumption. Strange out of memory issue while loading an image to a Bitmap object

Another option inJustDecodeBounds can help you to find correct inSampleSize value http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a

You should also consider using inPurgeable option http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inPurgeable. It allows system to free your memory to avoid OutOfMemory.

Upvotes: 2

Related Questions