Memory Management--Help Needed!

Hi all,

I’ve been using Corona since January or so to develop my thesis project and I’ve hit a problem that I’ve been unable to figure out. My project is the development of a game for use in a psychological study with kids and I’ve been using the Storyboard API to move between menu screens and into the actual game. Everything works just fine on the simulator (XCode & Corona) but when I build for my iPad, the game crashes with low memory errors.

www.protobear.com//assets/srice/projects/Pig%20Pandemonium.zip

The project can be found at that link though I won’t ask anyone to look at it (unless you’re feeling particularly helpful) as its not streamlined by any means. I’ll post relevant code snippets and my questions below.

I’ve been using the following lines of code in order to watch memory usage:

local memUsage\_str = string.format( "memUsage = %.3f KB. texture = %.3f", collectgarbage( "count" ), (system.getInfo( "textureMemoryUsed")/1000000) ) print(memUsage\_str);

I print this in enterScene so I can watch what my memory looks like as I move through the program. What I notice is this:

  1. Memory Usage (the first value printed out) is based on each individual scene alone. I.E. The title screen always takes ~162 KB of memory, the name input screen takes ~127, etc.

  2. The texture memory in use is additive. It seems as if my texture memory is never being released with each scene taking up additional memory until the app crashes on the device (where memory is limited).

This second issue seems to be where my app is having problems (obviously) but I’ve been unable to reduce my texture memory use. Upon entering each new scene, I purge the previous scene (which unloads the previous scene and frees its memory, at least as I understand it).

In each scene’s exitScene, I’ve also tried to unload my images and sprite sheets. I’ve attempted to do this in multiple ways:

randomObject:removeSelf(); randomObject = nil;

display.remove(randomObject); randomObject = nil;

In the case of spriteSheets:

display.remove(randomSpriteSheet); randomSpriteSheet = nil;

randomSpriteSheet:dispose(); randomSpriteSheet = nil;

I’ve scoured this site and any posts I could find on how to properly free memory but nothing I’ve tried seems to actually work. Am I missing some fundamental feature of unloading memory or is it possible that I’m on the wrong track altogether?

Any help you can give would be greatly appreciated!

Stephen

Note: For those interested in actually downloading and looking over my actual project, the file/scene order is: Main --> Title --> Name Input --> Character Select --> Waiting --> Versus --> Game. You may notice that I’m faking the connection to a second “player.” This is a feature of the actual psych study to make kids think they are playing against other kids instead of the computer.

Note 2: I also am aware that some of my code is inefficient or not in best practices (I just noticed the stickied optimization post and plan to adapt much of my code to those standards). I apologize for any winces my code may have caused! [import]uid: 97503 topic_id: 26717 reply_id: 326717[/import]

Hey Stephen,

I’m not sure whether or not this will be useful for you but I believe it may be - http://developer.anscamobile.com/forum/2012/05/21/guide-findingsolving-memory-leaks

As you have already noted, optimization is very important :wink:

Peach [import]uid: 52491 topic_id: 26717 reply_id: 108460[/import]

Thanks for the link, I’ll look it over and see what I can do!

Update: So the obvious thing I’m noticing is that I’m not canceling any of my timers (nor storing them in variables) and, now that I think about it, the first scenes that start crashing are ones that involve timers… Going to investigate that first. Barring that, the storyboard memory usage seems quite useful! Thanks again!
[import]uid: 97503 topic_id: 26717 reply_id: 108539[/import]

No luck. I’ve made sure all of my timers are being canceled in exitScene but I’m still getting the same crashes. The storyboard debug wasn’t working (it couldn’t find the function printMemUsuage() ) so I used the other set of code:

[code]local function printMemUsage()
local memUsed = (collectgarbage(“count”)) / 1000
local texUsed = system.getInfo( “textureMemoryUsed” ) / 1000000

print("\n---------MEMORY USAGE INFORMATION---------")
print(“System Memory Used:”, string.format("%.03f", memUsed), “Mb”)
print(“Texture Memory Used:”, string.format("%.03f", texUsed), “Mb”)
print("------------------------------------------\n")

return true
end

–Call it at the beginning of your new scene or the first line under your .new() function

printMemUsage(); [/code]

The output in the console when running this from each enterScene looks like the following. I’ve noted with a comment section where the game crashes on device.

---------MEMORY USAGE INFORMATION---------
System Memory Used: 0.163 Mb
Texture Memory Used: 12.584 Mb

---------MEMORY USAGE INFORMATION---------
System Memory Used: 0.122 Mb
Texture Memory Used: 16.780 Mb

---------MEMORY USAGE INFORMATION---------
System Memory Used: 0.190 Mb
Texture Memory Used: 46.173 Mb

---------MEMORY USAGE INFORMATION---------
System Memory Used: 0.238 Mb
Texture Memory Used: 117.501 Mb

–CRASH HERE

---------MEMORY USAGE INFORMATION---------
System Memory Used: 0.310 Mb
Texture Memory Used: 138.489 Mb

WARNING: Accelerometer events are only available on the device.
WARNING: Accelerometer frequency on iPhone must be in the range [10,100] HzWARNING: Simulator does not support accelerometer events

---------MEMORY USAGE INFORMATION---------
System Memory Used: 0.337 Mb
Texture Memory Used: 138.583 Mb

My system memory looks alright to my eye, it fluctuates with each scene but I can’t imagine that it’s problematic. Texture memory appears to be constantly increasing which is why I suspected that was my issue but I dont’ know that for a fact.

Can anyone explain what system.getInfo( “textureMemoryUsed” ) returns and what that means in terms of memory usage? Thanks! [import]uid: 97503 topic_id: 26717 reply_id: 108548[/import]

Yeah that is very high for texture memory; it looks like nothing is getting cleaned up.

I can’t personally review your code but I would think perhaps you weren’t inserting things into the group correctly - are all your images being inserted into the group created in each storyboard scene?

RE memory usage we actually have a thread that goes into detail on what the numbers mean and what the limits are on a number of devices that might be worth looking at; http://developer.anscamobile.com/forum/2012/01/08/acceptable-memory-usage

[import]uid: 52491 topic_id: 26717 reply_id: 108612[/import]

I ended up creating my own groups within each scene as I hoped that would make organization easier and give me finer control over what objects are visible or not. The group that is created as part of the storyboard API generally has an object or two in it but nothing important (earlier I was having crashes related to those groups being empty).

All of my other groups get loaded with images and the like and then are removed in destroyScene(). Would this type of setup prevent Corona from unloading texture memory?
[import]uid: 97503 topic_id: 26717 reply_id: 108731[/import]

So I did a quick test on my title screen which consists solely of a single background image and a start button. My original code created and used a new group called titleGroup with those two items in its hierarchy. I moved them both to the group in the storyboard API (the line is group = self.view) to see if they would unload properly. Unfortunately, there was no change in the amount of texture memory in use. [import]uid: 97503 topic_id: 26717 reply_id: 108753[/import]

That’s a little odd, I’m not a storyboard expert by any means but I believe that should have worked.

Can you tell me what build of Corona you are using (some people have multiple accounts, paid+test, so it pays to check) and whether or not you have these memory issues using the included Storyboard sample code? [import]uid: 52491 topic_id: 26717 reply_id: 108800[/import]

Sure. I’m using version 2011.704.

I ran the same memory usage code in the storyboard (called it in the EnterScene functions) and it looked like each scene was only using 3.201 MB of texture memory. I didn’t see any obvious leaks.

I was looking at how the storyboard example deals with objects though, and I wonder if perhaps I missed something obvious.

I create all of my variables inside of createScene and don’t label them as local so that I can access them from enterScene and my other functions. The storyboard example creates local variables in the space before createScene after the new storyboard functions have been called.

It was my understanding that my system was alright given Lua’s dynamic instantiation of variables, but could this be causing my issue? I ran a quick test by creating local variables at the top but it didn’t seem to make any difference.

The other thing I’m noticing is that the storyboard examples never explicitly remove images. They’re added to the screenGroup (which is equal to self.view) and then nothing else is ever done with them.

Thanks again for all of your help!
[import]uid: 97503 topic_id: 26717 reply_id: 108873[/import]

That wouldn’t be the issue, no - texture memory is unrelated.

My advice is to run through two scenes in your own app and adjust them to do things exactly like the example in terms of removing/clean up and see if the problem persists.

If it does, great - if it doesn’t then if you could upload a sample that shows the issue I might be able to take a look through it one evening. (Free time permitting.)

Let me know.

Peach :slight_smile: [import]uid: 52491 topic_id: 26717 reply_id: 108900[/import]

Hi Peach,

Sorry I didn’t get back to you earlier but it’s been a busy week. I’ve gone through the first two scenes of the storyboard example and my project and tried to make them match up as closely as possible. The only major differences that I am seeing are based on my project using sprite sheets where the storyboard example does not.

www.protobear.com/assets/srice/projects/Pig%20Pandemonium%20Test.zip

You can download the relevant files there. It only contains two scenes of the project (title and nameInput) but the memory leak should show up. NameInput should take about 4 MB of texture memory (if you go straight from main to nameInput, you will see this) and main should take 12 MB. When you follow the current order, however, there are 17 MB of texture memory taken up by the time you reach nameInput.

Thanks again for your help! [import]uid: 97503 topic_id: 26717 reply_id: 109558[/import]