I think it is asked and asked before, but still, there are things I can’t quite understand.
I have tried two different approaches:
- Keep all the images in memory, when certain limit is started to exceed, start removing them
- Let Android fix this with SoftReferences
In 2. it’s just cleaning them up sometimes the second I allocate them! And I don’t allocate too much – 30-40 images 50×50 pixels.
So I am sticking to one. The question is what is the limit?
- Can I get some reliable info from the device of how much exactly bitmap memory do I have left? I have done some research, watch DDMS values, it’s just taking up more and more space (if I don’t clean up) until it explodes. One moment there are only 200K left, next the system provides 2M more…
- I am currently using some heuristic decision, based on device model, or screen size. I think this is dead-end in the long run. Got memory exceptions on some phones, completely free on other.
- Is there a third solution, the right one?
Most important question: Are you
recycle()-ing your bitmaps? This is very important for pre-Gingerbread applications (maybe post-Gingerbread too).Watching the Memory Management for Android Apps session from Google I/O 2011 helped me get a better understanding of the peculiarities of developing for Android.
That video mentions a tool called MAT – a Memory Analyzer – which is useful for determining if you have objects hanging around in memory that have been leaked. It’s possibly you have more than the 30-40 you think you have.
For viewing and/or logging the current size of heap, etc., I’d suggest using the code in this answer about Android Out of Memory exceptions.