hi.
I was trying to understand how garbage collection works on corona, but still with no light at the end of the tunnel. my goal is if i understand it well enought i can create more robust and better “memory friendly” apps.
i tried with the basics:
local array={} for i=1, 10000 do array[i]="string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. string. " end local memUsedx local texUsedx local printMemUsage = function() memUsedx = (collectgarbage("count")) texUsedx = system.getInfo( "textureMemoryUsed" ) / 1048576 -- Reported in Bytes display.remove(memUsed) display.remove(texUsed) memUsed=nil texUsed=nil memUsed=display.newText({text=memUsedx, width=120}) memUsed.x=display.contentWidth\*.5 memUsed.y=display.contentHeight\*.5-50 memUsed:setFillColor(1,1,1) memUsed:toFront() texUsed=display.newText({text=texUsedx, width=120}) texUsed.x=display.contentWidth\*.5 texUsed.y=display.contentHeight\*.5+50 texUsed:setFillColor(1,1,1) texUsed:toFront() end if (system.getInfo("environment") == "simulator") then Runtime:addEventListener( "enterFrame", printMemUsage) end
in C when you declare an array wtih 10000 it will reserve memory for that array, even if you don’t put anything in it…its reserved to use for that array.
here in corona i don’t understand what he does. i declare it, i fill it but nothing happens…the memory is the same as before i declare and fill the array.
strange things further happens if i remove them from memory adding this code
local t local function removerArray() print ("delete array") for i=#array, 1, -1 do array[i]=nil end print (#array) array=nil timer.cancel(t ) t=nil end t=timer.performWithDelay(2000, removerArray,1)
now the memory grows like it should before i tried to remove it…and after i removed objects…the garbage collector pretty much ignored my request. I know that what i did was telling the garbage colletor that array is ready for deleting nothing more. it will clean memory at some point in the future when it needs more space. if i want to force the memory cleaning i need to do collectgarbage(“collect”). That worked fine. but why memory only grows if i tried to remove it? if i only declare and fill it why memore don’t grow? corona detects if the array is used somewhere if it’s not he doesn’t do nothing? can i conclude that lost variables that are not used anywhere will not grow 1 byte in memory? it should use space since i declare them and fill them.
regards,
Carlos

