I have 3 image resources in my app.
Loading and displaying any one of them in an ImageView goes smoothly.
But when I remove the displayed ImageView from the layout, null it and
add a new image, things get weird.
Either the process crashes completely (signal 11) when I try
calling .recycle() on the old bitmap or I get an OutOfMemoryError in
case I don’t use recycle.
This only happens on API Levels below 8, so my guess is that bugs with
GC on the native heap are causing it and these bugs were fixed on
2.2. However, since 2.1 is currently the most common version out
there I need a workaround…
Again, there shouldn’t be a lack of memory for the native heap. I hold
only 1 image at a time and null the previous so the GC should be able
to retrieve the heap space (like happens on 2.2).
I’ve created a very small sample app which can be downloaded from
here:
http://www.4shared.com/file/QqHrhJLR/BitmapRecycleTest.html
Any suggestions will be appreciated.
I’ve taken a look. I get the signal 11 on a Nexus One running 2.2.1. The callstack is this:
Clearly there’s a bug in Android here, but it seems to be avoidable.
Don’t use your BitmapResource class… you should explicitly recycle() your bitmaps the moment you don’t need them any more. I changed your DrawView constructor to this:
Since your images are so large – 7MB decompressed! – I also added a
System.gc()after the previous image is removed.buttonClickHandlernow looks like this:With these changes your app runs fine for me.