Android ListActivity with Bitmaps and Garbage Collection issue

Posted by chis54 on Stack Overflow See other posts from Stack Overflow or by chis54
Published on 2012-03-31T23:25:58Z Indexed on 2012/03/31 23:29 UTC
Read the original article Hit count: 263

I have a ListActivity and in it I set my list items with a class that extends SimpleCursorAdapter. I'm overriding bindView to set my Views. I have some TextViews and ImageViews. This is how I set my list items in my cursor adapter:

String variableName = "drawable/q" + num + "_200px";
int imageResource = context.getResources().getIdentifier(variableName, "drawable", context.getPackageName());
if (imageResource != 0 ) {
    // The drawable exists
    Bitmap b = BitmapFactory.decodeResource(context.getResources(), imageResource);
    width = b.getWidth();
    height = b.getHeight();
    imageView.getLayoutParams().width = (int) (width);
    imageView.getLayoutParams().height = (int) (height);
    imageView.setImageResource(imageResource);
} else {
    imageView.setImageResource(R.drawable.25trans_200px);
}

The problem I'm having is whenever I update my list with setListAdapter I get a large amount of garbage collection:

D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 125K, 51% free 2710K/5447K, external 2022K/2137K, paused 75ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 30K, 51% free 2701K/5447K, external 2669K/2972K, paused 64ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 23K, 51% free 2713K/5447K, external 3479K/3579K, paused 53ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 22K, 51% free 2706K/5447K, external 3303K/3352K, paused 64ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 21K, 51% free 2722K/5447K, external 3569K/3685K, paused 102ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 23K, 50% free 2755K/5447K, external 3499K/3605K, paused 65ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 23K, 50% free 2771K/5447K, external 4213K/4488K, paused 53ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 18K, 49% free 2796K/5447K, external 5057K/5343K, paused 75ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 28K, 49% free 2803K/5447K, external 5944K/5976K, paused 53ms
D/dalvikvm(  435): GC_EXPLICIT freed 6K, 54% free 2544K/5511K, external 1625K/2137K, paused 50ms
D/dalvikvm(  165): GC_EXPLICIT freed 85K, 52% free 2946K/6087K, external 4838K/5980K, paused 111ms
D/dalvikvm(  448): GC_EXPLICIT freed 1K, 54% free 2540K/5511K, external 1625K/2137K, paused 50ms
D/dalvikvm(  294): GC_EXPLICIT freed 8K, 55% free 2598K/5703K, external 1625K/2137K, paused 64ms

What can I do to avoid this? It's causing my UI to be sluggish and when I scroll, too.

© Stack Overflow or respective owner

Related posts about java

Related posts about android