Hello.
While playing with image sheets, i found that memory never drops to values it was before drawing images after i remove them, so i decided to inspect this closer, use loop to create newImage. Memory usage not depends on image size. Memory usage is growing, when loop count is growing, not growing, if loop count is less or equal than before, so it’s not memory leak, but not dropping down too. Why ? Why loop of 20000 is using more memory, than, let’s say, n * 15000, if run one after another… And why printMemUsage() is executed before collectgarbage(‘collect’) ends ?
display.setStatusBar( display.HiddenStatusBar ) --------------------------------------- display.setDefault( "anchorX", 0) display.setDefault( "anchorY", 0) --------------------------------------- local myRandom = math.random local function testLeak() local myImage = display.newImage( "image.png" ) --remove as recommended myImage:removeSelf( ) myImage = nil end; -- memory usage local function printMemUsage() local memUsed = (collectgarbage("count")) \* 0.001 local texUsed = system.getInfo( "textureMemoryUsed" ) \* 0.000001 print("\n---------MEMORY USAGE INFORMATION---------") print("System Memory Used: " .. string.format("%.03f", memUsed) .. " Mb") print("Texture Memory Used: " .. string.format("%.03f", texUsed) .. " Mb") print("------------------------------------------\n") return true end -- leak test local function goTestLeak() local x = myRandom(20000) print( "MEMORY BEFORE TEST: ") --garbage ? collectgarbage('collect') --not enough collectgarbage('collect') --still not enough collectgarbage('collect') --still not enough printMemUsage() for i = 1, x do testLeak() end --garbage ? collectgarbage('collect') --not enough collectgarbage('collect') collectgarbage('collect') print( "MEMORY AFTER TEST: ( " .. x .. " loops )") --memory printed before full garbage collector cycle ? printMemUsage() end -- tap to test Runtime:addEventListener( "tap", goTestLeak )
Best regards.