[Guide] Finding/Solving Memory Leaks

Because it’s a global way of creating modules, stay away from globals unless using a global really cuts down on coding, for example if you use the same module or table throughout your app then make it global.

Read this… https://coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/

I,m developing a framework to handle (cancel/destroy/stop/resume/etc) all timers/transitions/sounds/listeners, etc based on a module made by myself :slight_smile:

The idea is to call, when we want, a timer/transition/sounds/listeners and register them into the respective handler.

Finally, when the object is activated/deactivated/destroyed we have all timers/transitions/sounds/listeners into the handlers and we can find out them by  a unique key and do what we want.

This framework controls all object states, including variables creation/destroy, activation/deactivation, etc

It’s simple. With this, I dont have memory leaks and create new objects based on this framework is trivial

Any object is a group. With this we can create complex objects and perform rotation, hooks, etc on them as one.

My idea is to extend the framework to add  more stuff to control scenes, etc

Thanks

My opinion about globals:

The thing about globals is that you have to always be aware of them. Proper routines for deleting them when your´e supposed to.

(i.e. like a Runtime listener). This may seem obvious but it can still be a “silent member” of a tricky situation of debugging a chunk of code. My experience is that the beauty of lua´s lexical scoping and Coronas garbage handling makes you not use globals that much after you get the hang of it. I dont buy the argument about the memory cost…the time they wrote that stuff was when the phones didn´t handle huge amount of data…The real argument for locals is: why hardcode when you can smartcode? and it´s much more fun too!. I read up on local globals, globals, local variables in PIL (Programming In Lua 3.ed and I must say, it´s really good reading)

I know this should not be posted under memory leaks but it was just ment as a quote to the post over made by QuizMaster.

Btw, aren´t all external modules/classes globals? You can make private and public vars there but the stuff you return from the class is still placed in the _G environment right up to the point where you “unload/nil´s” the module

…please correct me if I´m wrong here. :slight_smile:

If I have a native field or any other display object and I attach an event listener, should I have to explicitly remove that listener before destruction to avoid a leak, or is that taken care of when I remove the display object automagically?

This is important because scene is a lexical global so it’s pretty easy to remove listeners bound to the scene variable available all over the scene. The downside is that some of my scene listener functions like scene:tap(event) get complicated.

bodyField:removeEventListener(“userInput”, scene) – do I really need this?

bodyField:removeSelf()

bodyField = nil

No you don’t have to. Code isn’t allocated memory. It compiled in your app already, it can’t grow any larger.

As far as what event listeners  you have to remove, it’s pretty much limited to anything you attach to the global Runtime (enterFrame most common). When you put a touch or tap listener on an object, if the object goes away, there is no way to interact with it. The touch handler you assigned is just a pointer in the object’s table that goes away when it get’s nilled out.

Rob

I’m using a lot of timers and transitions in my current game and recently noticed a scene where not much is happening is counting up a LOT of memory usage and I think this has to do with the growing number of timers and transitions used in the game.

I’m using my timers and transitions like this:

in main.lua I create a timerStash={} and a transitionsStash={}

then in the scenes and modules for example I’m using:

transitionStash[#transitionStash+1]=transition.to(object,{time=1000,alpha=1,onComplete=function() startFunction (obj.x,obj.y) end})

timerStash[#timerStash+1]=timer.performWithDelay(1000,function() startOtherFunc (myValue) end,1)

This usage grows the stashes like insane because I’m working on a Tower Defense game with a lot of objects, collisionDetection and more.

The stashes are in the thousands after a few seconds and I wonder if this can be a reason for a huge memory usage? And how to prevent this or a better solution to handle a lot of this transitions, timers in a Tower Defense game with a lot of objects and stuff going on?

@c.noeth

kinda obvious question but do you clean every object after you use it. For example it gets shot by some shot from your tower, you have to delete it. Other important thing is you have to delete the SHOT itself (unless you are making a continuous loop with the same objects so you don’t have to delete objects all the time.
 

what i mean is, if you continue building shots/enemies and never deleting them, timers/transitions referenced to them would never get deleted thus creating your problem.

solution? don’t use transactions at least :D, timers you will be forced but in MARGINALY smaller amount. I have game with lots of balls flying around and some defending paddles protecting middle, and memory usage is minimal where i use around 11 timers ( if i recall) and tons of balls coming from all sides (all powerd by physics)

start working with them and in time you will see how much better they are, and the possibilities they give you (f.e. you can make a weapon that deflects from an enemy walking on the path, towards the next enemy, possible with transactions too but way harder)

There are not so much objects and enemies and everything still sums up.

Thx for the info!

I have started to recreate the whole project while looking for the memory. I have noticed in an empty scene the memory also is counting up. Slowly but counting. Is this normal?

it shouldn’t sum up if everything is properly managed (tho its not a rule there can be an increase but in the end it should return similar numbers (1kb-8kb is nothing much to worry about everything above is considered bad)  )

Empty scene shouldn’t make an increase for sure so something bad is happening there, paste some code how u remove the timer/transition because that is the only thing that comes to mind that might make this problem

I have no timers and transitions added yet, so this maybe is the 1kb to 8kb “okay” part then. After watching this for a while now the memory falls back to a start val of about 0.780 MB. It counts up to about 1.6 MB and then falls back to around 0.780 MB.

that is alright :slight_smile: it just takes some time for system to get rid of whatever it was bugging it. its what i said in previous post, it can increase but in the end it should return to similar numbers(on empty scene tho to the exact numbers), on scene change it can happen sometimes that u change like 4 scenes, on first 3 it keeps increasing, then on 4th change it drops on the same numbers like before the first skip :smiley:

Because this is the expert area I want to ask if you can take a look at my post here:

https://forums.coronalabs.com/topic/74306-difference-in-system-memory-usage-on-device-and-simulator/

This one is about asking if there is a difference between the Corona Simulator and for example an iOS device (like an iPhone 6) ? Regarding the system memory usage?

Because I have no system memory leak in the simulator BUT on the device.

The game dumps unused resources from the RAM into a swap, “Winda” caches them and saves them in case of going to sleep, then the graphics are reused and re-deposited in the “long box”. But the old copies are not deleted anywhere - after a few hours, depending on the capacity of the drive, the free space on it ends, the system drops before rebooting and cleaning the temporary files. If the game did not try to “optimize” the consumption of RAM, unloading and re-pumping resources, it would flow as usual, with a gradual slowdown and subsequent crash.

Even I had the same issue thanks guys for valuable comments and solutions

My issue has been solved finally

Thanks 9apps app cartoon hd

Thank you soooo mucchhh!!! My issues are finally solved! one of the best forum! 9apps vidmate vlc

Thank you soooo mucchhh!!! My issues are finally solved! one of the best forum! 9apps vidmate vlc