I'm writing a program that involves a ton of images, over 100 per screen density. Fortunately, they aren't very big in terms of space. I use roughly 11 at any given time. I also use the function Bitmap.createScaledBitmap
on every image which seems to take up a lot of memory.
So far in debugging this app, I seem to be able to use it indefinitely without running into a memory problem, which I'm hoping means I'm not leaking memory.
However, the one thing I've noticed is that if I "exit" my app through the back button (I'm not running anything in the background), and then restart the app shortly after, I sometimes get a out of memory error at a call to Bitmap.createScaledBitmap
01-07 19:01:24.935: ERROR/AndroidRuntime(27419): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
So basically my question is, what am I doing wrong here? Do I need to be cleaning up my own garbage, like in onDestroy when the user presses the back button? I would have thought the GC would automatically take care of this when the back button was pressed and the activity was destroyed. This leads me to believe I am doing something else wrong, like a memory leak. But then I come back to the fact my app can take heavy memory use for 20 minutes but then kill itself in 5 seconds after a restart, which is what is baffling me.
Thanks all.
Edit: I implemented a couple quick and dirty fixes.
First I tried this, and it was harder to get the application to force close.
@Override
public void onBackPressed() {
finish();
}
Next I tried this combined with the onBackPressed and I could no longer duplicate my problem. Note that the method call is just a loop that basically does allImages = null;
protected void onStop () {
super.onStop();
mComponent.releaseImages();
}
At this point it appears that after calling finish(), there are still objects in memory from the activity. Rather strange.