Memory usage

Greetings -

I’m using this function found on these forums to monitor memory:


function checkMemory()
   collectgarbage( “collect” )
   local memUsage_str = string.format( “MEMORY = %.3f KB”, collectgarbage( “count” ) )
   print( memUsage_str, "TEXTURE = "…(system.getInfo(“textureMemoryUsed”) / (1024 * 1024) ) )
end
timer.performWithDelay( 1000, checkMemory, 0 )


When I start a scene (level1), the ‘Memory’ reads out to be about 450KBs - then when I go to a different scene (menu) and go back to the same level1 scene - the ‘Memory’ reads out to be 500KBs.  It seems to increase by about 50KBs each time I revisit the ‘level1’ scene - even if I’m not doing any actions on the scene and immediately go back to menu then revisit.  I really been cautious to cancel timers, remove and set objects to nil, use local variables, standard garbage collection stuff as seen in tutorials.

Is this increase in Memory a definite memory leak or I’m misinterpreting the function.  The ‘Texture Memory’ does not increase.  It runs smooth (60 fps) no matter how many times I go back to the level1 scene.

It’s caused me to stop my project until I can find an answer!  The memory seems so minimal that I don’t know whether it’s a normal increase or will cause issues later.
 

Hi @henson802,

The most important thing to check is whether the memory continues to increase over and over and over, just creeping up a little each time you navigate around scenes. It’s normal for an app to start at a certain memory usage level, then increase a slight amount. However, over time, it should stabilize to about that level (with minor fluctuations). If you notice that the memory continues to creep upward, then you definitely have a memory leak and you need to fix it.

Hope this helps,

Brent

@Brent Sorrentino

Thanks for the reply.  I have heard this answer before and I don’t quite understand.  It’s not really ‘creeping up’ per say - but when I revisit the scene - the memory reads around 40-50 KBs higher then it was when I left.  But when I’m in the scene - the memory isn’t increasing.

So if I go from Menu to Level1 - the memory reads 450 KBs.  I don’t do anything, hit a button to go back to Menu- and Memory now reads 440 KBs.  Don’t do anything, hit the button to go to Level1 again- and Memory is 490 KBs.  Rinse and repeat - when I go back to Menu - it’ll be 480 KBs - then back to Level1 it’s 530KBs.  Go back to Menu - 520KBs then to Level1 570KBs.  See what I’m saying?

I haven’t really kept track past 700 KBs but it does this same pattern - adds about 50 KBs when I revisit level1 scene.

When I’m in the scene - and not doing anything - the Memory will NOT creep up. 

So this would be a memory leak? 

Appreciate your help - I’m just trying to be clear and understand so I’m not trying to fix a memory leak that may not be one or moving on when I should be fixing!

Hi again,

Basically, what I mean by “memory creep” is that it continues to climb and never goes back down. So, let’s say you set up two basic scenes that don’t actually do much, and you just keep moving from one to the next, back and forth.

If you monitor the memory, let’s assume two example outputs (each value being the approximate value you see when you are within that scene):

450 > 500 > 520 > 510 > 500 > 494 > 502 > 520 > 510 > 505 > 515 > etc.

That does not indicate any memory leak, since the number fluctuates in the ~500 range as you move from scene to scene, and it sometimes goes up, but sometimes goes back down.

But this:

450 > 500 > 520 > 550 > 580 > 600 > 640 > 689 > 722 > 786 > 804 > etc.

That is definitely a memory leak, and something is not being cleared properly, thus the memory level continues to creep upward and it never goes back down.

Hope that clarifies it,

Brent

Yes then I guess I got a memory leak.  Argh - I cancel my timers, remove / nil all my objects, this been creeping me up the wall - I’m assuming when you have a ‘sceneGroup’ that you put all your objects in , then change sceneGroup = scene.view on switch - that is changing the objects to nil as well as removing…

I just don’t get how the memory can creep up when I’m not even doing anything in the scene.   Just hitting button to switch from one scene to another.

Well anyways - thanks for the clarification @Brent Sorrentino. :) 

Hi @henson802,

Are you using Composer? If so, have you read through the Composer Library guide, in specific regarding scene management?

http://docs.coronalabs.com/guide/system/composer/index.html#scene-management

Note that scenes aren’t necessarily destroyed/removed when you switch to another scene, and also note the list of things which you need to manually clean up for proper memory handling. I’m not sure what is causing your memory leak, but make sure you don’t have any stray variables or references hanging around. That’s probably not the most useful advice, but the blunt truth is that tracking down memory leaks is an ordeal that all developers struggle with. :confused:

Take care,

Brent

Hey @Brent Sorrentino-

I appreciate the follow up. I have read thru that previously.

After looking thru my code again, I managed to find 2/3 of the memory leak issue.

They were quite simple things as particle effects emitters weren’t being removed and set to nil. I hadn’t thought about it since they disappeared visually.

And the other was I had used the ‘composer.removeScene’ on a button within the scene. So I don’t think it was fully removing, but have now since just used that button to ‘goToScene’ - then once the next scene is loaded- I get the previous scene name and use the ‘composer.removeScene’ then.

That has helped a lot - now when I go back and forth between scenes without doing anything else, memory remains the same ( it was jumping up 50 each time before).

I still have a little memory leak but have a better idea as to where/how it’s happening… It has to do with objects being destroyed, which I remove and set to nil - but I believe they maybe referenced later.

Hopefully that’s it, going to check it out after my ‘primary’ job heh. I feel like I’m on the right track tho, it’s been a learning experience for sure. Want to get it under control before moving on with this project. Thanks again, I’ll try to post more info so maybe it can help others at some point

Hi @henson802,

The most important thing to check is whether the memory continues to increase over and over and over, just creeping up a little each time you navigate around scenes. It’s normal for an app to start at a certain memory usage level, then increase a slight amount. However, over time, it should stabilize to about that level (with minor fluctuations). If you notice that the memory continues to creep upward, then you definitely have a memory leak and you need to fix it.

Hope this helps,

Brent

@Brent Sorrentino

Thanks for the reply.  I have heard this answer before and I don’t quite understand.  It’s not really ‘creeping up’ per say - but when I revisit the scene - the memory reads around 40-50 KBs higher then it was when I left.  But when I’m in the scene - the memory isn’t increasing.

So if I go from Menu to Level1 - the memory reads 450 KBs.  I don’t do anything, hit a button to go back to Menu- and Memory now reads 440 KBs.  Don’t do anything, hit the button to go to Level1 again- and Memory is 490 KBs.  Rinse and repeat - when I go back to Menu - it’ll be 480 KBs - then back to Level1 it’s 530KBs.  Go back to Menu - 520KBs then to Level1 570KBs.  See what I’m saying?

I haven’t really kept track past 700 KBs but it does this same pattern - adds about 50 KBs when I revisit level1 scene.

When I’m in the scene - and not doing anything - the Memory will NOT creep up. 

So this would be a memory leak? 

Appreciate your help - I’m just trying to be clear and understand so I’m not trying to fix a memory leak that may not be one or moving on when I should be fixing!

Hi again,

Basically, what I mean by “memory creep” is that it continues to climb and never goes back down. So, let’s say you set up two basic scenes that don’t actually do much, and you just keep moving from one to the next, back and forth.

If you monitor the memory, let’s assume two example outputs (each value being the approximate value you see when you are within that scene):

450 > 500 > 520 > 510 > 500 > 494 > 502 > 520 > 510 > 505 > 515 > etc.

That does not indicate any memory leak, since the number fluctuates in the ~500 range as you move from scene to scene, and it sometimes goes up, but sometimes goes back down.

But this:

450 > 500 > 520 > 550 > 580 > 600 > 640 > 689 > 722 > 786 > 804 > etc.

That is definitely a memory leak, and something is not being cleared properly, thus the memory level continues to creep upward and it never goes back down.

Hope that clarifies it,

Brent

Yes then I guess I got a memory leak.  Argh - I cancel my timers, remove / nil all my objects, this been creeping me up the wall - I’m assuming when you have a ‘sceneGroup’ that you put all your objects in , then change sceneGroup = scene.view on switch - that is changing the objects to nil as well as removing…

I just don’t get how the memory can creep up when I’m not even doing anything in the scene.   Just hitting button to switch from one scene to another.

Well anyways - thanks for the clarification @Brent Sorrentino. :) 

Hi @henson802,

Are you using Composer? If so, have you read through the Composer Library guide, in specific regarding scene management?

http://docs.coronalabs.com/guide/system/composer/index.html#scene-management

Note that scenes aren’t necessarily destroyed/removed when you switch to another scene, and also note the list of things which you need to manually clean up for proper memory handling. I’m not sure what is causing your memory leak, but make sure you don’t have any stray variables or references hanging around. That’s probably not the most useful advice, but the blunt truth is that tracking down memory leaks is an ordeal that all developers struggle with. :confused:

Take care,

Brent

Hey @Brent Sorrentino-

I appreciate the follow up. I have read thru that previously.

After looking thru my code again, I managed to find 2/3 of the memory leak issue.

They were quite simple things as particle effects emitters weren’t being removed and set to nil. I hadn’t thought about it since they disappeared visually.

And the other was I had used the ‘composer.removeScene’ on a button within the scene. So I don’t think it was fully removing, but have now since just used that button to ‘goToScene’ - then once the next scene is loaded- I get the previous scene name and use the ‘composer.removeScene’ then.

That has helped a lot - now when I go back and forth between scenes without doing anything else, memory remains the same ( it was jumping up 50 each time before).

I still have a little memory leak but have a better idea as to where/how it’s happening… It has to do with objects being destroyed, which I remove and set to nil - but I believe they maybe referenced later.

Hopefully that’s it, going to check it out after my ‘primary’ job heh. I feel like I’m on the right track tho, it’s been a learning experience for sure. Want to get it under control before moving on with this project. Thanks again, I’ll try to post more info so maybe it can help others at some point