views:

92

answers:

3

I have an Android app that in the onCreate() method, preloads a lot of graphics.

When I test my app on my HTC Aria and launch it, it runs fine. However, if I press the back button to exit my app, and then launch the app again, it crashes with an OutOfMemoryError: bitmap size exceeds VM budget. If I then launch the app for the third time (right after it has crashed) it launches fine. Then if I close and re-launch it, it crashes again with out of memory. It continues this every-other-time-crashing pattern forever if I keep trying.

I checked to see what life cycle methods were being called and onStop() and onDestroy() are both being called when I exit the app, yet I have a feeling that something is not yet being cleaned up and that by "crashing" the app when I try to launch it the second time, it somehow free's the memory.

Any thoughts on what could be happening or how to remedy this? Please let me know if you need me to post more info. Thanks!

Info:
My app is fairly simple and only has 1 activity that plays some frame animations.

A: 

It sounds like something in the Activity life cycle is not quite right. Are you sure you have every start covered? http://developer.android.com/reference/android/app/Activity.html

You have onStop but do you have onDestroy? You're probably missing one of those that you need.

Aidanc
Thanks Aidanc, what would I need to put in onDestroy to clean up the memory properly? Right now I'm loading a handful of graphics into AnimationDrawable objects.
justinl
+2  A: 

Maybe you are unnecessarily holding onto Context references? Check Avoiding memory leaks for some tips, as well as Attacking memory problems.

JRL
I'm going to investigate into this. So far I've basically nulled out every single member variable in my whole application. And I've set all the Drawable callbacks to null but I'm still getting the same problems. I will try using the MAT and see if I can get some more info
justinl
Fixed it! That Attacking memory problems link was an excellent read on how to properly use the MAT Analyzer tool. Turns out I had some views and I had to set the background resource to 0 like this: myView.setBackgroundResource(0); If you do not do this explicitly the system will still hold onto that drawable resource even if you set the view to null.
justinl
I'd like to also add that if you've defined any layouts in your code, it's a good idea to call myLayout.removeAllViews(), otherwise they can maintain references to things they shouldn't after your app has closed.
justinl
A: 

You may find some useful information the many answers to this question: http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue

Also, I second the "Avoiding Memory Leaks" blog post. Especially if you can trigger the same problem with orientation changes. Using "this" context when creating display objects is a sneaky way to leak the Activity context. In my own app, I managed to leak a whole chain of contexts, and would very rapidly run out of memory when doing orientation changes.

BenTobin