A memory (garbage collection)question

I have one scene I’m testing that’s relatively simple.  I’ve gone through the tutorial on optimization and garbage collection.

It consists of a player throwing bombs at other objects - and they both die (remove then nil) soon after collision.

I’m running this code to test for leaks:


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


Now it all works handy dandy but my question is once you are in the scene - is it normal for MEMORY to slowly increase when you perform an action and not go back down once action is complete?

In my example, a character spawns a bomb when you touch screen - toss - hits an object.  Then both the object and bomb blow up then get removed. 

My logic would lead me to believe that the memory would increase a little then once I remove the two objects, memory count will go back down.  But that’s not the case for me - am I experiencing memory Leak or misinterpreting how Memory is used?

Below is the garbage collection code results when I’m spawning/tossing bombs and they collide -  remove the bomb and whatever it collides with. 

 MEMORY = 495.419 KB TEXTURE = 2.7714881896973

 MEMORY = 495.864 KB TEXTURE = 2.7714881896973

 MEMORY = 496.841 KB TEXTURE = 2.7714881896973

 MEMORY = 496.632 KB TEXTURE = 2.7714881896973

 MEMORY = 496.397 KB TEXTURE = 2.7558631896973

 MEMORY = 498.450 KB TEXTURE = 2.7714881896973

 MEMORY = 499.708 KB TEXTURE = 2.7714881896973

 MEMORY = 499.890 KB TEXTURE = 2.7714881896973

 MEMORY = 500.856 KB TEXTURE = 2.7714881896973

 

The Memory will keep counting up even when I remove the two objects (bomb and what its colliding with) and make them Nil.  I thought that memory would eventually go back down once the bomb and object collide since I’m removing them.

 

NOTE - And IF I stop throwing bombs - then the Memory Count will remain at the last value, not increasing.

 

IF I keep throwing bombs - the Memory keeps increasing despite them being removed along with the objects they collide with.  IF I don’t throw bombs then memory will remain the same.

Yes it is normal, if you are not forcing Lua to collect (and free) garbage.

Lua doesn’t return memory right away.  It frees up memory when it hits a certain memory used trigger point.  This allows Lua to re-use that memory if it wants, and to save on the thrash cost associated with frequent allocations and de-allocations.

However, if you are forcing garbage collection, and you are seeing a continuous rise (that never declines) as you create and destroy objects, then I suspect you’re not releasing them properly, or you’ve a leak.

Can you share your object creation, tracking, and removal code?

Great -

Thanks for the reply!

Wanted to make sure before moving onto more scenes …

I’m curious because when I start level, before throwing any bombs- it stays around 450 … Then as I start spawning snd throwing bombs - it increases by one or two. Typically I’m removing the bomb and what it collided with in three or so seconds from spawning/throwing.

I can spawn / throw / remove bombs and objects continually , I’ve gone up to 550 KBs… Would it ever go back to 450 at start if I stopped spawning / throwing at 550 … I’ve stayed idle and it’ll just remain at 550 - doesn’t seem to go back to original value of 450 when I started level, even tho now there’s less objects.

Sorry I’m new trying to understand memory management :slight_smile:

I’d keep an eye on that.  Something doesn’t sound quite right, but without seeing code I can’t be sure.

If all objects in the scene are owned by the scene, not referenced, elsewhere, and you leave the scene, AND if the scene’s destroy method is activated, those objects should be removed.

Even then, if you don’t force garbage collection, the memory usage may stay the same.  HOWEVER, if you force garbage collection, at this point memory usage should be lower than before the forced collection.

I’d be especially worried if, after going back and forth between scenes, memory continued to rise.

Remember, show your code if you can to get the best help possible.

Right… Well I’m headed to sleep then work. I’ll try to post the code after- I want to look at it again. It could very well be a leak. I have a walking animation and that has about 4 frames from a spritesheet which doesn’t add to memory when I walk with character and it animates.

So in theory- if you have a scene that starts with a memory of 450 KBs- then after a few minutes I have destroyed and removed objects in scene - the memory should be less then 450 KBs?

Thats what’s happening in my scene… Player spawns bombs, throws, then blows up whatever it collides with. Ultimately you end with less objects then you start, but my memory is higher…?

I’ll try to get the code up cause I think it has some holes- using table to track the bombs, etc.

Yes it is normal, if you are not forcing Lua to collect (and free) garbage.

Lua doesn’t return memory right away.  It frees up memory when it hits a certain memory used trigger point.  This allows Lua to re-use that memory if it wants, and to save on the thrash cost associated with frequent allocations and de-allocations.

However, if you are forcing garbage collection, and you are seeing a continuous rise (that never declines) as you create and destroy objects, then I suspect you’re not releasing them properly, or you’ve a leak.

Can you share your object creation, tracking, and removal code?

Great -

Thanks for the reply!

Wanted to make sure before moving onto more scenes …

I’m curious because when I start level, before throwing any bombs- it stays around 450 … Then as I start spawning snd throwing bombs - it increases by one or two. Typically I’m removing the bomb and what it collided with in three or so seconds from spawning/throwing.

I can spawn / throw / remove bombs and objects continually , I’ve gone up to 550 KBs… Would it ever go back to 450 at start if I stopped spawning / throwing at 550 … I’ve stayed idle and it’ll just remain at 550 - doesn’t seem to go back to original value of 450 when I started level, even tho now there’s less objects.

Sorry I’m new trying to understand memory management :slight_smile:

I’d keep an eye on that.  Something doesn’t sound quite right, but without seeing code I can’t be sure.

If all objects in the scene are owned by the scene, not referenced, elsewhere, and you leave the scene, AND if the scene’s destroy method is activated, those objects should be removed.

Even then, if you don’t force garbage collection, the memory usage may stay the same.  HOWEVER, if you force garbage collection, at this point memory usage should be lower than before the forced collection.

I’d be especially worried if, after going back and forth between scenes, memory continued to rise.

Remember, show your code if you can to get the best help possible.

Right… Well I’m headed to sleep then work. I’ll try to post the code after- I want to look at it again. It could very well be a leak. I have a walking animation and that has about 4 frames from a spritesheet which doesn’t add to memory when I walk with character and it animates.

So in theory- if you have a scene that starts with a memory of 450 KBs- then after a few minutes I have destroyed and removed objects in scene - the memory should be less then 450 KBs?

Thats what’s happening in my scene… Player spawns bombs, throws, then blows up whatever it collides with. Ultimately you end with less objects then you start, but my memory is higher…?

I’ll try to get the code up cause I think it has some holes- using table to track the bombs, etc.