Discrepancy between On Device memory usage and Simulator memory usage

I’ve been working with Corona a few months now and I’ve realized maybe it’s time to start optimizing my code to run within the RAM constraints of some lower performing devices. (Up until now I’ve just been testing on my phone which has a fast CPU and plenty of RAM). My app runs great on the phone – smooth, high frame rate, no crashes.

Anyway, I decided to check out how much RAM the app is using on my phone. Upon startup, it’s around 30MB. That’s just the main menu with a background image and a couple of buttons.

Seems kind of high but no biggie. I noticed that if I don’t touch anything and switch back and forth between the app and Android settings (to see the ram usage) the RAM actually starts to go up – 32MB, 34 MB, 37MB…and it just keeps going up as I switch back and forth without actually touching anything in the app.

Once I get to the meat of the game the usage goes up even more 6 to 8MB at a time, without even interacting with the objects in the level (everything is static, no animations or anything). The RAM usage went past 100MB until I decided to stop checking.

Eventually I think the garbage collector kicks in and the usage goes down, but slowly starts to increase again.

So I looked at the storyboard.printMemUsage function in the simulator to see what’s going on. This is the printout that I got (during the main portion of the game where you actually play):

---------MEMORY USAGE INFORMATION--------- 2014-07-14 14:23:51.727 Corona Simulator[2840:507] System Memory Used:    0.833    Mb 2014-07-14 14:23:51.727 Corona Simulator[2840:507] Texture Memory Used:    2.940    Mb 2014-07-14 14:23:51.728 Corona Simulator[2840:507] ------------------------------------------

The numbers fluctuate as expected when I interact with the game and new objects get spawned, but the System Memory stays below 1Mb and Texture Memory stays less than 3Mb (I am taking care to remove display objects and nil out their references).

Enough of the boring stuff, here are my questions:

  1. Why does the device show that the RAM usage is going up so much even though I’m not interacting with anything?

  2. Why is the info from the storyboard.printMemUsage function nowhere near the amount of RAM being shown on the phone?

I can post the code from one of my scenes if anyone is interested.

The memory reported is “texture” memory, i.e. the memory used by graphics  textures.  System memory is memory allocated to Lua variables (strings, numbers, tables).  This doesn’t show the memory used by code (or the Corona core) nor does it show any memory allocated to audio, video, etc.

I’m a bit confused about your memory increase.  You say you’re not interacting with the app but you say you’re going back and forth.  That makes me think you are changing scenes.  Perhaps you’re changing scenes and loading audio but not disposing it and when you enter the scene again you load it again.  How fast is it going up?

Hi Rob, thanks for responding and clearing up a couple things.

When I say switching back and forth I mean going from my app to the Android settings to see the running processes and then back to my app. I’m not changing scenes in this scenario, the app opens up to the same state I left it.

Do the scene events get fired every time you enter and exit the app? For example, I tap the icon for my game and tap through a couple scenes to reach the Level 1 Scene. Then I press the home button to go to my phone’s home screen. Then I tap the icon for my game again and it opens directly to the Level 1 Scene (in the same state as it was before I pressed home).

One other thing, I am using physics touch joints as my means of moving objects around when the user touches them. Some of those objects also have collision event listeners. Is it possible that the physics engine uses up RAM when it’s sitting idle (i.e. when I’m not touching anything and nothing is colliding)?

The scenes should not be getting re-entered.  However  you could have something in your onResume code.

The memory reported is “texture” memory, i.e. the memory used by graphics  textures.  System memory is memory allocated to Lua variables (strings, numbers, tables).  This doesn’t show the memory used by code (or the Corona core) nor does it show any memory allocated to audio, video, etc.

I’m a bit confused about your memory increase.  You say you’re not interacting with the app but you say you’re going back and forth.  That makes me think you are changing scenes.  Perhaps you’re changing scenes and loading audio but not disposing it and when you enter the scene again you load it again.  How fast is it going up?

Hi Rob, thanks for responding and clearing up a couple things.

When I say switching back and forth I mean going from my app to the Android settings to see the running processes and then back to my app. I’m not changing scenes in this scenario, the app opens up to the same state I left it.

Do the scene events get fired every time you enter and exit the app? For example, I tap the icon for my game and tap through a couple scenes to reach the Level 1 Scene. Then I press the home button to go to my phone’s home screen. Then I tap the icon for my game again and it opens directly to the Level 1 Scene (in the same state as it was before I pressed home).

One other thing, I am using physics touch joints as my means of moving objects around when the user touches them. Some of those objects also have collision event listeners. Is it possible that the physics engine uses up RAM when it’s sitting idle (i.e. when I’m not touching anything and nothing is colliding)?

The scenes should not be getting re-entered.  However  you could have something in your onResume code.

For anyone else who comes across this, it looks like this issue was reported as a bug:

33402 (Open) Memory Leak for each onSuspended and onResumed action

The issue seems to have been resolved in the latest public build 2014.2393 (2014.8.5).

For anyone else who comes across this, it looks like this issue was reported as a bug:

33402 (Open) Memory Leak for each onSuspended and onResumed action

The issue seems to have been resolved in the latest public build 2014.2393 (2014.8.5).