views:

186

answers:

3

I have an Android game that has an activity for the menu, and then another activity for the game that creates a SurfaceView and Thread to deal with canvas drawing and game logic. When you exit the game and start it up again too much or if you open and close the keyboard (thus restarting the activity), the game runs out of memory, usually when loading a bitmap:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

How can I keep all my images in memory without loading them again when the game changes state, or how can I release them from memory and let them reload when the game is restarted?

A: 

How are you loading and releasing the images currently?

Found this: http://osdir.com/ml/Android-Developers/2010-04/msg02411.html

"Try not to load the image into a byte array before passing it to the BitmapFactory but rather pass the InputStream directly to the BitmapFactory. There is a limit for the size of an image."

Darxval
+1  A: 

have you set android:launchMode in your manifest? If you haven't and your not explicitly handling returning to the original activity in onResume/onStart your likely getting multiple copies of your game activity running at the same time.

You can look at the various options for launchMode here

Mark
I tried every combination of launchMode for my two activities, but the problem is still happening.
sirconnorstack
A: 

Your context may be leaking. This should help http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html

Fedor