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.
bindView is a critical method when animating a list. It gets called when the ListView is scrolled and new items are loaded in because they are appearing. bindView gets called on the UI thread which means animation cannot continue until it finishes. Usually this isn’t a problem because you’re just assigning variables. In this code you’re calling
Bitmap b = BitmapFactory.decodeResource(context.getResources(), imageResource);which has to hit the NAND flash which is expensive. You can use StrictMode to detect these kinds of problems by the way.Bitmaps do get unloaded from memory when no longer used but it’s not immediate, the GC has to get called. Android offers a way to drop the large byte array data that bitmaps carry with the Bitmap.recycle() method. That will hopefully solve the
OutOfMemoryError's.If the approaches above don’t work for some reason then you’ll have to deal with a more complex solution which involves threads and callbacks.
Let me know if you need help with that. If you can please clarify why you need the width and height of the image?