out of memory help

Started by Disastorm, August 07, 2012, 09:34:51 AM

Previous topic - Next topic

Disastorm

Hello,

I'm making a game and it can start the first time fine, but after I close the Activity and start it back up, it gets an Out Of Memory error.  I unload all my textures, dispose the world and the buffer in the OnStop() method.  What is the best way for me to resolve this error, do I really need to decrease the memory usage, even though the first startup can start fine? Or perhaps I should just make my application kill the process when you quit, although I heard thats not a good thing to do in android?

EgonOlsen

Just do what the examples do (or something silimar), i.e. don't dispose anything except for the FrameBuffer but store the current state so that i can be resumed if the Activity comes back to live. jPCT-AE will care about uploading texture, mesh and shader data on context change.

Disastorm

What if I want to dispose all of the textures and world though to load a new level or something like that?

EgonOlsen

In that case, what you are doing sounds fine. Make sure that you also remove your textures from the manager, not just unload them, which wouldn't work in onStop() anyway, because the gl context is long lost at that point and memory reserved within the old gl context will be freed anyway if the context gets lost.
However, it doesn't hurt to call it. You might want to give setting Config.unloadImmediately=true; a try too.

EgonOlsen

Also keep in mind that, in case you are reusing instance variables, that


world=new World();


is not the same as


world=null;
world=new World();


The former one will use twice the memory while creating the new instance of World (or Object3D or whatever...) while latter won't.

Disastorm

Hm that helped a bit but now it gets out of memory on the third start up after stopping twice. Its weird thouh because it says the "Memory usage after compacting" is only just under 3000KB but then I get an error saying "VM won't let us allocate 131072 bytes" "OutOfMemoryError: bitmap size exceeds VM budget"

Disastorm

#6
Quote from: EgonOlsen on August 07, 2012, 10:51:20 AM
Also keep in mind that, in case you are reusing instance variables, that


world=new World();


is not the same as


world=null;
world=new World();


The former one will use twice the memory while creating the new instance of World (or Object3D or whatever...) while latter won't.

really? Is this only in the case of making a World or for all objects? I have actually been setting objects to null to try to reduce memory usage but I wasn't sure if it was actually affecting anything or not.  Why does not setting the world to null first use twice the memory? wouldnt the old World have nothing pointing to it so it should be cleaned up?

*edit oh do you mean it uses twice the memory until after the new world is created than the memory usage goes back down?

EgonOlsen

Quote from: Disastorm on August 07, 2012, 11:01:36 AM
*edit oh do you mean it uses twice the memory until after the new world is created than the memory usage goes back down?
Yes, that's what i meant and no, it has nothing to do with World itself. It's the same thing for all objects and it's caused by the way Java works. I just wanted to mention it, because it may matter for large objects.

Disastorm

Do you know what error this is: "OutOfMemoryError: bitmap size exceeds VM budget" And why would this happen when the Memory Compactor says im only using 3000KB of memory?

Is that related to textures?

EgonOlsen

#9
No, not directly. It's related to Bitmaps. As mentioned in the other recent thread, different versions of Android take different memory types into account when reporting the free memory. This message refers to the Android bitmap cache being full. How are you loading your textures? If you are not using input streams but Bimap directly, make sure to recycle the Bitmap after it's no longer needed. (Bitmap.recycle()). How large (dimensions, not file sizes) are these Bimaps/Textures anyway? 

Disastorm

Thanks I didnt think I was using BitMaps directly but I realized that the AGLFont and TexturePack classes that Raft made create BitMaps and I was recreating the AGLFont object each time.  I set it to static and only create it once and I no longer get the memory issue.

EgonOlsen


Disastorm

I dont know what happened but I get the out of memory error again when i restart my game.  Does the phone allocate exact amount of ram to an app or does the app share memory with other stuff?

EgonOlsen

I've noticed some erratic behaviour with the Bitmap-cache in some older Android versions, where starting the same app sometimes caused an OOM when trying to load the bitmaps. I never really figured out why this happens. The compact()-method in MemoryHelper was a sirty workaround for this. But even that didn't help all the time.
In your case, it might help to fire up some debugging tools to see where the memory goes on restart. Maybe you or jPCT-AE leaks some memory somewhere.