Need clarity about memory handling

8a. Timers and transitions functions will clean themselves up after they fire or canceled. You should nil any variables that contain the handles returned from the timers/transitions after they finish in order to free up the variables themselves.

8b. Canceling a timer from within the listener will deallocate the timer.

event.source within a listener is a local variable that goes away when the listener function ends. There is no need to nil the variable. [import]uid: 7559 topic_id: 34463 reply_id: 139296[/import]

@Rob @Tom: Thanks again! I am relieved that non-assigned timers vanish. Now, I tried doing garbage collection each step of the way to see which statements make a difference, replacing the deprecated gcinfo() with collectgarbage("count").

[lua]function gc(num)
collectgarbage(“collect”)
print(num … ": " … collectgarbage(“count”))
end

function listener(event)
gc(3)
if event.count >= 2 then
timer.cancel(event.source)
gc(4)
event.source = nil
gc(5)
end
end

function test()
gc(1)
timer.performWithDelay(100, listener, 3)
gc(2)
end

test()[/lua]

My output was:
1: 119.046875
2: 119.6171875
3: 119.6796875
3: 119.6796875
4: 119.6796875
5: 119.6796875

If we are to trust these numbers, the cancelling and nilling makes no difference at all. How could this be? Doesn’t the actual memory cleanup happen before after the Lua parser exits the scope of the listener function, perhaps?
[import]uid: 73434 topic_id: 34463 reply_id: 139303[/import]

Hi Olav,
In my experience, printing out memory usage can be a bit misleading. Just doing a garbage collection doesn’t necessarily show an instant result… sometimes it takes several frame cycles to reflect the new value.

What I do, in testing memory, is to run a 1-second repeating timer of my memory printout, then I run my game and play each “stage” of it… open up the menu, load scenes, exit scenes, return to menu, restart the level, etc. While doing that, I look for “patterns” in the memory readout… typically there will be a value that you’ll begin to recognize. For example, when I go through 10-15 cycles of “load-exit-load-exit” or “restart-restart-restart”, one number will surface as a “common” value no matter how many times I repeat the cycle. When that happens, I’m quite convinced that I’m cleaning up everything properly… because the number is -exactly- the same over a dozen iterations of the gameplay.

Now, if the value kept gradually creeping up in value, then I’d suspect my memory was leaking somehow.

Brent [import]uid: 200026 topic_id: 34463 reply_id: 139341[/import]

I figured as much. Anyway, now that all the details about good memory habits are on the table, I can do the last bit of cleanup and submit my game. I also plan to write a document about my general experiences with Corona that I will send you guys. Thanks again! [import]uid: 73434 topic_id: 34463 reply_id: 139440[/import]

Hi Olav,
That would be greatly appreciated (a document from a direct developer standpoint about some memory cleanup procedures, testing methods, etc.). When you have it ready, just e-mail it to Rob or myself.

Thanks, and best of luck with it!
Brent [import]uid: 200026 topic_id: 34463 reply_id: 139483[/import]