Memory usage going up and down is normal, especially during scene transitions because things like variables being created, new objects being created, brand new image files being loaded, etc. all contribute to higher memory usage.
When memory usage goes down slightly, it most-likely means some things were removed and then when memory usage seems to go down significantly then most-likely it means that Lua’s garbage collector kicked in (which removes all variables, etc. that are set to nil and have no references).
Garbage collection in Lua is automatic, and is pretty unpredictable, which is why you’ll see inconsistent behavior in terms of when the rising and falling of memory usage occurs. So that’s all normal behavior.
Also, in the storyboard sample, the memory usage that is outputted to the screen doesn’t represent an accurate account of memory usage for that scene, but rather, during that very moment when the text was printed. The best way to monitor memory usage is through an enterFrame listener, with the usage being printed out on every frame.
Once you have that in place, what you want to look out for is if while performing the same actions over and over again (over several different iterations), if memory usage is climbing and falling at about the same rate. If you notice that no matter how much times you repeat the same actions over and over again, that memory is continuing to climb (the lows continue to get higher and higher), then that could indicate a memory leak somewhere in the code of the actions you were performing.
Here’s a real-world example.
I was recently working on a project and decided to monitor memory usage once a certain feature was complete. The feature involved touching an object, and then being kicked over to a new screen to do something related to that object.
So to test for memory leaks I touched the object, and then touched the button that would bring me back to the new screen. Exited the screen, and kept repeating that process as I watched memory usage.
I began to notice that while memory usage would always go back down once I exited the secondary screen, the memory drop would always result in memory usage being 1kb higher than it was the previous time I repeated the steps.
Just in case, I made a mental note of that and just kept repeating the same exact actions and memory usage just kept going up by a mere 1kb every time I cycled through that action (and I made sure not to do anything “new” in my steps, to rule out the possibility of some other code causing a climb in memory usage).
I knew then, that there was a memory leak somewhere within that workflow. Sure enough, I “audited” the code that was responsible for those actions I was taking, made a few changes, and I was able to squash the small 1kb memory leak! (and a few other memory leaks related to some similar workflows).
And for those interested, this is the code I used to catch the memory leak (just stick it at the end of main.lua):
local function garbagePrinting() collectgarbage("collect") local memUsage_str = string.format( "memUsage = %.3f KB", collectgarbage( "count" ) ) print( memUsage_str ) local texMemUsage_str = system.getInfo( "textureMemoryUsed" ) texMemUsage_str = texMemUsage_str/1000000 texMemUsage_str = string.format( "texMemUsage = %.3f MB", texMemUsage_str ) print( texMemUsage_str )endRuntime:addEventListener( "enterFrame", garbagePrinting )[/code] [import]uid: 52430 topic_id: 23230 reply_id: 93192[/import]