My game has developed a hefty memory leak out of (seemingly) nowhere so I’ve been looking at a lot of articles about good memory management, including this fantastic memory checklist:-
http://developer.coronalabs.com/node/26628
However, in the course of scouring the forums I’ve got a few questions which I don’t feel are properly answered (at least to this rookie - possibly I don’t understand the explanations I’ve read!).
-
Timers / transitions. Assigning a variable to a looping timer or transition, so that you can cancel it later, makes a lot of sense. But is it definitely the case that even single-use timer.performWithDelay uses should be assigned variables and then cancelled / nil’ed? Am I right in thinking that it’s not necessary to cancel these timers, but if I don’t give them a variable and nil them they might lock up some small part of the memory which won’t be garbage collected?
-
Cancelling listeners. The checklist says to always remove listeners between scenes. Am I right in thinking that this rule only applies to listeners created or called from within the scenes themselves? I’m trying to separate my code into more independent modules, and this relies heavily on runtime events. So for example I might have a module governing a rule table that goes something like this:-
– function create() which creates display object and transitions it onto the screen
– function destroy() which transitions it off screen and destroys all the display objects.
Runtime:addEventListener(“showRules”, create)
Runtime:addEventListener(“hideRules”, destroy)
The events “showRules” and “hideRules” may be dispatched by the scene, but they’re not part of the scene code.
Am I right in thinking that those listeners are fine to just sit there indefinitely, assuming I’m comfortable with the amount of memory being taken up by that module generally? They don’t need to be removed do they?
- Closures. I think I’ve finally grasped the concept of closures and now I’m terrified that I’m storing lots of up values which aren’t being gc’d. I had (for example) been nesting local functions within local functions in circumstances where the nested function only needed to be called by the enclosing function, and therefore it looked “tidier” to nest them in this way. I don’t think when I’ve been doing this I’ve been taking advantage of closures at all, but am I right in thinking that this risks occupying memory which is difficult to clear?
Thanks for any tips.
Matt