Memory & Texture Memory Questions

OK, I’m in the middle of making my game and I have the following screens:

-Main Menu
-Settings
-Level Select
-Level 1

In the main.lua file I have the following function to monitor the memory and texture memory in use:

[lua]local monitorMem = function()

collectgarbage(“collect”)
print( "\nMemUsage: " … collectgarbage(“count”) )
local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000
print( "TexMem: " … textMem )

end

local memTimer = timer.performWithDelay(1000, monitorMem, -1)[/lua]

I’m trying to work out if and where I’m leaking memory and need some advice please. I’ve being pulling my hair out over this for a few days now trying to figure all this out.
When I open the main.lua Igo straight to the Main Menu and here’s the output:

MemUsage: 236.72265625
TexMem: 2.686976

If I go to the Level Select screen from here, this is the new output:

MemUsage: 269.76171875
TexMem: 3.227648

Now, if I return to the Main Menu from here:

MemUsage: 249.0576171875
TexMem: 2.686976
The texture memory returns back to the initial starting point (good), but personally, I would have expected the memory usage to return to the initial level but it has gained 13 (is this Kb?).

Does this signify a memory leak? What exactly is the MemUsage and what happens if it climbs? Is Texture Memory more important?
Continuing on…from the Main Menu I go back to Level Select:

MemUsage: 275.8671875
TexMem: 3.227648

Then on to Level 1:

MemUsage: 287.41015625
TexMem: 2.613248

Back to the Main Menu:

MemUsage: 285.908203125
TexMem: 2.686976
Please can someone throw some light on this…

[import]uid: 74503 topic_id: 18063 reply_id: 318063[/import]

Assuming you are using the Storyboard API for scene changes?

If so, it all depends on whether or not you are purging the previous scene or removing it.

The small climb in memory every time you switch a scene is most-likely due to the fact that when a new module is loaded, there’s a reference to it created in the global package.loaded table.

When you first went to Level Select screen and then back to the Main Menu and saw a 13kb jump, that is most-likely due to the fact that the module is still loaded in memory (even if all the display objects have been removed—which explains why texture memory went back down to norma—and everything in the module has been completely cleaned out).

With storyboard, the only way to completely unload a module (including its reference in package.loaded) is to use storyboard.removeScene()).

Currently, there is a bug where storyboard.removeScene() does NOT remove the reference from package.loaded, but in the next Daily Build (after 2011.689), the issue has been fixed. [import]uid: 52430 topic_id: 18063 reply_id: 69038[/import]

The real test you want to do is this - if you cycle back and forth between main menu and level select (or any two scenes) does the memory continue to climb?

It’s likely that you’re just seeing a one-time bump in memory, whether it be an improperly unloaded scene as Jonathan suggests, or something else. But if you can flip back and forth without constant increase, you should be fine.

Test each combination, then see if just flipping around between scenes over and over causes continual climb. [import]uid: 65996 topic_id: 18063 reply_id: 69101[/import]

Hi Jonathan,

Yes, I am of course using the Storyboard API - that’s probably the reason why the Texture Memory usage is being so well managed throughout.

As you can see, I just have this problem with with the Memory Usage climbing.

When I have been experimenting, trying to rectify this, I switched to calling storeboard.removeAll() in every scene to completely unload any that may have been previously loaded. I thought that the issue with the package.loaded bug had been resolved in the latest build (2011.689). Is this bug still present in this build then?

Also, to remove any external modules from package.loaded do I do the following within exitScene():

[lua]package.loaded[“timerModule”] = nil[/lua]

Or is there something else I need to do?

@simon.starnge: Unfortunately, flipping between Main Menu and Level Select increases by around 13kb the first time and then by about 6kb for each successive ‘loop’. It seems I see a similar outcome for other scene combinations also.
Within the exitScene() function, which is called immediately before a transition to any other scene, I remove audio, event listeners, transitions and external modules (see above). On entering the next scene I immediately call storeboard.removeAll(), and then I’ve even been doing collectgarbage(“collect”)…so I’m scratching my head as to where the leaks are.
Any ideas what else I can do/try?

Also, what exactly is the MemUsage and what happens if it climbs? Is Texture Memory more important?

[import]uid: 74503 topic_id: 18063 reply_id: 69117[/import]

@iNSERT.CODE: The storyboard.removeAll() fix will be in the Daily Build *after* 2011.689, so that *could* be your problem, BUT, that wouldn’t explain why memory usage goes up 6kb every time you go through everything.

When I switch back and forth between different scenes with the Storyboard SampleCode (which will also be included in the next daily build, after 2011.689), I’m not seeing the memory usage continue to climb (past a certain point, its always bouncing back and forth between a few ranges), so the culprit has to be within your code somewhere.

Also, due to delays with garbage collection in Lua, I found that it’s better to have your memory usage counter in an enterFrame listener at the bottom of main.lua, that way you can monitor realtime memory usage (see the Storyboard samplecode, main.lua). [import]uid: 52430 topic_id: 18063 reply_id: 69128[/import]

@jonathanbeebe: Thanks for getting back again. I’ll continue to look through my code to find where the problem lies and wait for the next build to see if that helps any too.

With regard to removing external modules, is what I’m doing in the previous post correct?

Also, a what point (in terms of Memory Usage) would a device lag and/or crash? Say, an iPhone 4? [import]uid: 74503 topic_id: 18063 reply_id: 69133[/import]