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)