Calling collectgarbage() every frame

Hello.

I want to clean my texture memory every frame, so I call collectgarbage() with an enterFrame eventListener., like this:

local label = display.newText("", 70, 310, "GROBOLD",4) function texto() collectgarbage() label.text="Lua Memory: " .. collectgarbage("count") .. " Texture Memory: " .. system.getInfo("textureMemoryUsed")/1000 end Runtime:addEventListener("enterFrame", texto)

Is it normal? Will I have any performance issues in the future by breaking its cycle and calling it every frame?

Don’t do this - Garbage Collection is pretty intensive and will have a detrimental effect on your performance. Usually the standard lua GC is pretty smart; however you could manually set it at certain points - clearing a level, scene transition, etc…

Exactly my sentiments - the collectgarbage() function is quite slow, and you can tell the effect even with the default garbage collector (which, I believe, runs every 500 ms - you can see the lag when you have things like a bunch of particles on screen).

You’re better off just letting the default garbage collector do it, or, like SegaBoy said, doing it when you leave a level or such (although even that isn’t really required).

Caleb

I agree with both SegaBoy and Caleb.  That said, it’s not a bad idea to have a function like that, displaying memory usage every frame, to help you catch accidental memory leaks as soon as you introduce them.  Just be sure to disable it before you release your final version!

  • Andrew

Thank you for your replies!

Don’t do this - Garbage Collection is pretty intensive and will have a detrimental effect on your performance. Usually the standard lua GC is pretty smart; however you could manually set it at certain points - clearing a level, scene transition, etc…

Exactly my sentiments - the collectgarbage() function is quite slow, and you can tell the effect even with the default garbage collector (which, I believe, runs every 500 ms - you can see the lag when you have things like a bunch of particles on screen).

You’re better off just letting the default garbage collector do it, or, like SegaBoy said, doing it when you leave a level or such (although even that isn’t really required).

Caleb

I agree with both SegaBoy and Caleb.  That said, it’s not a bad idea to have a function like that, displaying memory usage every frame, to help you catch accidental memory leaks as soon as you introduce them.  Just be sure to disable it before you release your final version!

  • Andrew

Thank you for your replies!