Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

[Guide] Finding/Solving Memory Leaks
Started by Danny May 21 2012 12:27 AM

* * * * * 3 votes
72 replies to this topic
[TOPIC CONTROLS]
Page 3 of 3 1 2 3
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#51

jack95

[GLOBAL: userInfoPane.html]
jack95
  • Contributor

  • 197 posts
  • Corona SDK

Do all transitions and timers leak memory if they are not explicitly tracked? 

 

I am under the impression, that they do. Currently every scene with transition has to have a _transition = {} table to keep all my transition references. Same with _timers = {}. In libraries and a lot of code I see, people don't seem to track them. 



[TOPIC: post.html]
#52

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,061 posts
  • Enterprise

They should not....

 

Rob



[TOPIC: post.html]
#53

QuizMaster

[GLOBAL: userInfoPane.html]
QuizMaster
  • Contributor

  • 235 posts
  • Corona SDK

Here's my experience of timers and transitions..

 

It's not memory leaks that you should be concerned with as long as you cancel all the timers and transitions.

 

You have to be wary of them still running when moving onto a different scene though.

Just because you nil a timer or transition doesn't mean it can't be created again by another timer or transition, you have to be diligent.



[TOPIC: post.html]
#54

jack95

[GLOBAL: userInfoPane.html]
jack95
  • Contributor

  • 197 posts
  • Corona SDK

It's not memory leaks that you should be concerned with as long as you cancel all the timers and transitions.

 

See, it's that kind of statement that leads me to a conclusion. 

If I don't capture the timers and transitions, how can I cancel them on destruction?

If I have to capture them all, to explicitly destroy, then they are inherently leaky.



[TOPIC: post.html]
#55

QuizMaster

[GLOBAL: userInfoPane.html]
QuizMaster
  • Contributor

  • 235 posts
  • Corona SDK

That's not what I'm saying.

 

I'm just pointing out that if you don't cancel all your timers and transitions before you move onto your next scene you could run into problems. If your previous scene only has a couple timers then they are easy to track.

But what if you have a more complex scene with many many timers and transitions, then you might not know when they all finish doing their job, some could be triggered by other timers, it's all about making sure everything is stopped before you move on to your next scene and claiming the memory back by nilling them so you don't get memory creep.

 

For example, shipTransition = transition.to (spaceShip, {time = 2000, x = x + 1000, onComplete = doSomething} )

 

After 2 seconds the function doSomething is called, the function doSomething could have more timers and transitions in it, the shipTransition maybe called via a timer or another transition over and over and so and so on and that's just one simple part of the code. What if you've got lots of spaceships and aliens?

 

You have to track all your timers and transitions otherwise you'll end up in a right mess, it's got nothing to do with memory leaks.



[TOPIC: post.html]
#56

luisxpto32

[GLOBAL: userInfoPane.html]
luisxpto32
  • Observer

  • 16 posts
  • Corona SDK

I'm having a HUUUGE memory leak ... I'm cleaning every local variable in my code and I can free about 20 ~ 30 of memory! And its using about 600 bytes of memory in my scene 

 

Using the profiller tool in mode 4 it shows me that the variables that are using most of memory are those of the array of the spritesheet, the ones generated by the function newSpriteSheetFromData(). I'm removing the spriteSheet with dispose() , but even so the memory isn't cleaned ... there is anyway of cleaning this array?

 

Another question ... i'm using "module (..., package.seeall)" to create classes and to assess variables in this classes i'm using set and get methods. The profiller tells me that some of this gets and setters are consuming a considerable amount of memory! There's any way to clean the memory associated with this functions? 

 

Thanks for the help! 

 

Hi!

Where can I find the "profiller tool"?

 

Thanks

 



[TOPIC: post.html]
#57

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,061 posts
  • Enterprise

First of all, you've not proven you have a leak. You have proven you are using memory. Those are two different things. All apps use memory. Memory can grow as the app does more and more things. Leaks are when you repeat the same code (i.e. spawn objects over and over,  but don't increase the number of objects), or repeat scenes over and over and memory keeps growning when it shouldn't.

 

Also we don't know what scale your memory is listed as. If it's really "bytes", 600 is nothing.  Even if it were 600 K (thousand, well 1024 = 1K) bytes, that's still nothing in the scale of the device.

 

You may be fretting over nothing.

 

Rob



[TOPIC: post.html]
#58

luisxpto32

[GLOBAL: userInfoPane.html]
luisxpto32
  • Observer

  • 16 posts
  • Corona SDK

First of all, you've not proven you have a leak. You have proven you are using memory. Those are two different things. All apps use memory. Memory can grow as the app does more and more things. Leaks are when you repeat the same code (i.e. spawn objects over and over,  but don't increase the number of objects), or repeat scenes over and over and memory keeps growning when it shouldn't.

 

Also we don't know what scale your memory is listed as. If it's really "bytes", 600 is nothing.  Even if it were 600 K (thousand, well 1024 = 1K) bytes, that's still nothing in the scale of the device.

 

You may be fretting over nothing.

 

Rob

 

 

Hi,

I'm repeating the same scenario over and over and the system memory increments 1Kb on each scene changes. If I comment the timer/transition everything is OK. 

 

I'm clear the handle of both timer/transition. What is going on?

 

Thanks



[TOPIC: post.html]
#59

QuizMaster

[GLOBAL: userInfoPane.html]
QuizMaster
  • Contributor

  • 235 posts
  • Corona SDK

You're gaining 1k on each scene change? You've got nothing to worry about then.

 

What you should be worried about is using "module (..., package.seeall)"



[TOPIC: post.html]
#60

luisxpto32

[GLOBAL: userInfoPane.html]
luisxpto32
  • Observer

  • 16 posts
  • Corona SDK

You're gaining 1k on each scene change? You've got nothing to worry about then.

 

What you should be worried about is using "module (..., package.seeall)"

 

It is necessary to do that? Why?



[TOPIC: post.html]
#61

QuizMaster

[GLOBAL: userInfoPane.html]
QuizMaster
  • Contributor

  • 235 posts
  • Corona SDK

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/



[TOPIC: post.html]
#62

luisxpto32

[GLOBAL: userInfoPane.html]
luisxpto32
  • Observer

  • 16 posts
  • Corona SDK

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 :-)

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



[TOPIC: post.html]
#63

Hendrix000007

[GLOBAL: userInfoPane.html]
Hendrix000007
  • Contributor

  • 177 posts
  • Corona SDK

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. :)



[TOPIC: post.html]
#64

jack95

[GLOBAL: userInfoPane.html]
jack95
  • Contributor

  • 197 posts
  • Corona SDK

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


[TOPIC: post.html]
#65

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,061 posts
  • Enterprise

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



[TOPIC: post.html]
#66

c.noeth

[GLOBAL: userInfoPane.html]
c.noeth
  • Contributor

  • 218 posts
  • Corona SDK

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?



[TOPIC: post.html]
#67

pasidaips

[GLOBAL: userInfoPane.html]
pasidaips
  • Observer

  • 12 posts
  • Corona SDK

@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)



[TOPIC: post.html]
#68

c.noeth

[GLOBAL: userInfoPane.html]
c.noeth
  • Contributor

  • 218 posts
  • Corona SDK

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?



[TOPIC: post.html]
#69

pasidaips

[GLOBAL: userInfoPane.html]
pasidaips
  • Observer

  • 12 posts
  • Corona SDK

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



[TOPIC: post.html]
#70

c.noeth

[GLOBAL: userInfoPane.html]
c.noeth
  • Contributor

  • 218 posts
  • Corona SDK

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.



[TOPIC: post.html]
#71

pasidaips

[GLOBAL: userInfoPane.html]
pasidaips
  • Observer

  • 12 posts
  • Corona SDK

that is alright :-) 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 :D



[TOPIC: post.html]
#72

d.mach

[GLOBAL: userInfoPane.html]
d.mach
  • Contributor

  • 872 posts
  • Corona SDK

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.

 

 



[TOPIC: post.html]
#73

bchad4734

[GLOBAL: userInfoPane.html]
bchad4734
  • Observer

  • 1 posts
  • Corona SDK

Yes anywhere from 1kb>8kb is nothing to be concerned about. If you go nuts like I did on my personal project, you can get it lower. Currently my own game leaks 1kb of memory and gains that 1kb back after a few scene changes. So basically I am leaking 1kb every 3>4 scene changes. essayservice

uid: 84637 topic_id: 26628 reply_id: 107915

 

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.




[topic_controls]
Page 3 of 3 1 2 3
 
[/topic_controls]