nilling variables... increases lua memory

Hey guys I just purchased Corona Profiler and it is an INCREDIBLE tool. I can not stress how useful it is. In fact it lead me to creating this post.

So basically in my game I have some variables that are local to enterScene(declared as locals within enterScene) => therefor I cannot nil them in exitScene or destroyScene, correct?

My solution: made my own function called “nilVariablesFunc” which is declared outside of listener functions and the variables within enterScene are within its scope. Withing that function I nil the variables and call the function on any scene change(Menu or restart).

While going back to the menu after pausing the game, Corona Profiler as promised shows everything that is going on in the report. I noticed that when nilling the variables with my custom function the lua memory rises by 20 kb! Shouldn’t it be going down instead!

I made sure that the variables are in fact in-scope by doing a simple “print(variable)” test.

Calling collectgarbage(“collect”) at the end fo the function does nothing.

However after returning to the scene each time the startup memory is always the same so I assume there are no memory leaks.

It’s not much of an issue but it is VERY puzzling to me. [import]uid: 118482 topic_id: 28605 reply_id: 328605[/import]

Sorry I can’t embed the image as Google picasa isn’t giving me a link…

Here is the direct link though:
https://picasaweb.google.com/116584639950662970890/CoronaProfiler#5764506753453095762 [import]uid: 118482 topic_id: 28605 reply_id: 115275[/import]

In general, local variables die when you leave the scope. They are effectively nil’ed automatically. However, if the local variable is a reference to something else in the system that is not local, your local variable will be cleaned up, but the thing it was referencing may or may not be cleaned up depending on if and how other things might be still referencing it.

I can’t really tell what’s going on in your program, but it is possible in creating your nil function, you inadvertently created a non-local reference to the things you were actually trying to release. Don’t forget that if you pass in a variable through as function parameter, those variables are essentially new local variables that are independent of the ones you passed in. (For tables/userdata, they reference the same underlying object, but are in fact different local variables.) My guess is your function doesn’t actually work the way you think it does. If you aren’t passing in variables as a function parameter, then you need to reference them as upvalues and/or global variables. In this case, this is where you may have inadvertently created a new strong reference to the objects you were hoping to collect.
[import]uid: 7563 topic_id: 28605 reply_id: 115415[/import]

Hmm ok so if I have some local variables withing enterScene(I declare them for the first time in enterScene and as locals) should I just leave them when I switch scenes and they will die by themselves? Or do you have another suggestion on how to remove them from memory? [import]uid: 118482 topic_id: 28605 reply_id: 115420[/import]

There are two different things: the local variables and the objects they reference.

The local variables will clean up themselves. The objects they reference may need to manually be cleaned up depending on what they are. If they are just Lua tables or normal Lua types, then it is all automatic and you don’t have to do anything. If they are Corona objects that document you have to do something special like call dispose(), then you will need to keep a reference around that you can access later so you can dispose() them later.

[import]uid: 7563 topic_id: 28605 reply_id: 115428[/import]

Alright cool thanks, they are just string, boolean, and number variables which I assume are “normal lua types.” Thanks for the clarification! [import]uid: 118482 topic_id: 28605 reply_id: 115429[/import]