Puzzled by Memory Management

Hey!

For all the games that I have been a part of in creating using Corona, there have been some memory leaks that I, nor anyone I work with have been able to address, so I thought that I’d ask if anyone in here had an answer.

local i local object = {} local function create() for i = 1, 50000 do object[i] = display.newRect( math.random(0,320), math.random(0,480), math.random(0,50), math.random(0,50) ) end end timer.performWithDelay( 1000, create, 1) local function destroy() for i = 1, 50000 do object[i]:removeSelf() object[i] = nil end end timer.performWithDelay( 5000, destroy, 1)

Using the code above, the project starts out with using approximately 191 KB of memory. After the create function has created objects 1 through 50,000, the project is using approx. 8293 KB of memory. Finally, after the destroy function has run and supposedly deleted all 50,000 objects, the project’s memory usage stagnates at at approx. 1213 KB. Texture memory usage does drop immediately back to zero after all objects have been deleted.

My team and I are working on a fast paced game in which the player will likely often transition between the game and the menu. Each time that happens, the game’s memory usage permanently increases by 1-7 KB.

My question is: what am I doing (or thinking) wrong with the code above? How can I ensure that all objects variables, etc. that I create are completely removed and their memory is freed so that my future games do not suffer such memory leaks?

ps
I am using the code below to check my memory usage:
 

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(250,checkMemory,0)

I’m seeing the same numbers, in an otherwise blank project. It’s probably just the garbage collector reaching a steady state.

Is this actually causing you problems, or just making you leery? It’s possible you could tune it (see here).

Lua Memory Usage (as I understand it)

  • Once your Lua memory allocation goes UP, it won’t necessarily come back DOWN to the starting point.
  • That is not a leak.

If however, you have a continuous non-declining rise that would be a leak.

UPDATE: I testing a modified version of your code (see below) and there are no leaks.

UPDATE2: I tested your code too.  Again no leaks.

I’ve modified your test and am NOT seeing any leaks:

local objects = {} local count = 50000 local function create() for i = 1, count do local tmp = display.newRect( math.random(0,320), math.random(0,480), math.random(0,50), math.random(0,50) ) objects[tmp] = tmp end end local function destroy() for k,v in pairs( objects ) do display.remove(v) end objects = {} end local maxIter = 50 local curIter = 0 local function doTest() curIter = curIter + 1 print(curIter) if( curIter \> maxIter ) then return end create() timer.performWithDelay( 500, destroy) timer.performWithDelay( 1000, doTest) end timer.performWithDelay( 3000, doTest)

I tested your code (with a minor tweak to make it re-runnable) and there is no leak.

See my code and the way I’m measuring here:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/05/leaktest.zip

In case it isn’t clear.

  1. Download the example.

  2. Run it with maxIter set to 1

See what the final memory usage is (for me it was about 1.26 MB)

  1. Change maxIter to 25, 50, 100, …

  2. Re-run it.  

See what the final memory usage is (for me it was about 1.26 MB)

If the memory usage for 1 or 2 runs is the same as 50 there is no leak.

StarCrunch, this is simply making me leery.

roaminggamer, thank you for your clear and detailed responses. I am glad to know that this is simply how Lua manages memory.

I’m seeing the same numbers, in an otherwise blank project. It’s probably just the garbage collector reaching a steady state.

Is this actually causing you problems, or just making you leery? It’s possible you could tune it (see here).

Lua Memory Usage (as I understand it)

  • Once your Lua memory allocation goes UP, it won’t necessarily come back DOWN to the starting point.
  • That is not a leak.

If however, you have a continuous non-declining rise that would be a leak.

UPDATE: I testing a modified version of your code (see below) and there are no leaks.

UPDATE2: I tested your code too.  Again no leaks.

I’ve modified your test and am NOT seeing any leaks:

local objects = {} local count = 50000 local function create() for i = 1, count do local tmp = display.newRect( math.random(0,320), math.random(0,480), math.random(0,50), math.random(0,50) ) objects[tmp] = tmp end end local function destroy() for k,v in pairs( objects ) do display.remove(v) end objects = {} end local maxIter = 50 local curIter = 0 local function doTest() curIter = curIter + 1 print(curIter) if( curIter \> maxIter ) then return end create() timer.performWithDelay( 500, destroy) timer.performWithDelay( 1000, doTest) end timer.performWithDelay( 3000, doTest)

I tested your code (with a minor tweak to make it re-runnable) and there is no leak.

See my code and the way I’m measuring here:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/05/leaktest.zip

In case it isn’t clear.

  1. Download the example.

  2. Run it with maxIter set to 1

See what the final memory usage is (for me it was about 1.26 MB)

  1. Change maxIter to 25, 50, 100, …

  2. Re-run it.  

See what the final memory usage is (for me it was about 1.26 MB)

If the memory usage for 1 or 2 runs is the same as 50 there is no leak.

StarCrunch, this is simply making me leery.

roaminggamer, thank you for your clear and detailed responses. I am glad to know that this is simply how Lua manages memory.