1

I have been struggling with this yesterday whole day, and still couldnt figure it out so I thought its time to SO it. So I have an app which has two listviews as fragments in two activities, one per tab.

I have two emulators open : 2.3.3 with 64 sd card, 2.2 with 64 sd card

and one device : samsung galaxy with 1GB sd card

When I run it on 2.3.3 emulator : I go to first list, scroll it down. Every row has an image that is lazy loaded from url using the DrawableBackgroundDownloader class that can be found here :

Lazy load of images in ListView

Then I go to second list and start scrolling down. GC already kicked in in first list but now it shows errors after first few items appear.

This is the output when it starts showing errors : enter image description here

the "downloading image" means that the image is being downloaded in thread as in mentioned class.

I have control over the images so I know that each list has about 40 items, and all images for both lists have altogether around 2MB

Heap size stays more less the same ~3.5MB but as you can see the external alloc cap jump from 10MB earlier to 12MB and 14244Kb then 16291Kb then 18328Kb and then suddenly it fails to allocate.

What is happening here?

Because this is 2.3.3 If I use MAT and analyze heap dump here. It has only 2.8MB. I know that bitmaps are not in heap (only references to it) therefore it is hard for me to analyze the leak there. Anyway it doesnt seem to be connected with heap but with external memory (gc says so and there is no OOME in emulator)

Histogram from heap on errors

enter image description here

When I run it on 2.2 device - it works and same actions dont cause GC to fail external allocation. However after succesfully scrolling second list to the end, sample from GC looks like that :

03-16 15:30:25.440: D/dalvikvm(19622): GC_EXTERNAL_ALLOC freed 6910 objects / 836600 bytes in 40ms

Why it is expressed as objects here? 836600 = ~ 816kb what does it mean?

When I run it on 2.2 emulator I am also getting GC errors but a bit later than in 2.3.3 case. I will add that changing sd card to 512 in emulator makes no difference.

Im still new to Android, I will appreciate any hints or pointing out errors in my reasoning

Community
  • 1
  • 1
AndroidGecko
  • 14,034
  • 3
  • 35
  • 49

3 Answers3

0

You have a memory leak and should recycle every image you don't use. Maybe also downscale them to the dimentions of each imageview . I've been stuggling with that issue too long my self too. How to effectively manage X amount of images. Keep track of every bitmaps you have and know when they are assigned to an imageView, then recycle all the others.

If you know how many images and their dimentions then you can calculate than you can handle for e.g 50 images at any given moment.

Get your hands in an ICS phone or a Honeycomp Tablet. Bitmap externatl memory allocation is visible in mat and you can figure out where you leak.

weakwire
  • 9,284
  • 8
  • 53
  • 78
0

This was something recommended to me a while back. Never got to using it but a lot of people liked it.ListViewImageManager

Phix
  • 9,364
  • 4
  • 35
  • 62
0

Thank you guys for your suggestions; I ended up using this great piece of code for handling images, HIghly recomended https://github.com/nostra13/Android-Universal-Image-Loader

AndroidGecko
  • 14,034
  • 3
  • 35
  • 49