Author Topic: out of memory help  (Read 6587 times)

Offline Disastorm

  • long
  • ***
  • Posts: 161
    • View Profile
out of memory help
« on: August 07, 2012, 09:34:51 am »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory help
« Reply #1 on: August 07, 2012, 10:18:13 am »
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.

Offline Disastorm

  • long
  • ***
  • Posts: 161
    • View Profile
Re: out of memory help
« Reply #2 on: August 07, 2012, 10:20:28 am »
What if I want to dispose all of the textures and world though to load a new level or something like that?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory help
« Reply #3 on: August 07, 2012, 10:48:18 am »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory help
« Reply #4 on: August 07, 2012, 10:51:20 am »
Also keep in mind that, in case you are reusing instance variables, that

Code: [Select]
world=new World();

is not the same as

Code: [Select]
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.

Offline Disastorm

  • long
  • ***
  • Posts: 161
    • View Profile
Re: out of memory help
« Reply #5 on: August 07, 2012, 10:58:09 am »
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"

Offline Disastorm

  • long
  • ***
  • Posts: 161
    • View Profile
Re: out of memory help
« Reply #6 on: August 07, 2012, 11:01:36 am »
Also keep in mind that, in case you are reusing instance variables, that

Code: [Select]
world=new World();

is not the same as

Code: [Select]
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?
« Last Edit: August 07, 2012, 11:05:17 am by Disastorm »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory help
« Reply #7 on: August 07, 2012, 11:19:15 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.

Offline Disastorm

  • long
  • ***
  • Posts: 161
    • View Profile
Re: out of memory help
« Reply #8 on: August 07, 2012, 11:25:36 am »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory help
« Reply #9 on: August 07, 2012, 12:28:46 pm »
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? 
« Last Edit: August 07, 2012, 12:36:36 pm by EgonOlsen »

Offline Disastorm

  • long
  • ***
  • Posts: 161
    • View Profile
Re: out of memory help
« Reply #10 on: August 07, 2012, 08:43:12 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory help
« Reply #11 on: August 07, 2012, 09:14:06 pm »
I'm glad that it helped!

Offline Disastorm

  • long
  • ***
  • Posts: 161
    • View Profile
Re: out of memory help
« Reply #12 on: August 10, 2012, 07:08:42 am »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory help
« Reply #13 on: August 10, 2012, 07:30:56 am »
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.

Offline Disastorm

  • long
  • ***
  • Posts: 161
    • View Profile
Re: out of memory help
« Reply #14 on: August 10, 2012, 07:58:45 am »
I wonder if the erratic behavior is related to this guy's theory http://stackoverflow.com/questions/6349943/android-bitmaps-recycling-and-heap-fragmentation