I noticed something strange with timer.performWithDelay and the app’s memory usage.
I monitored the memory usage with the following code:
local function checkMemory() collectgarbage("collect") local memUsageStr = string.format("MEMORY = %.3f KB", collectgarbage("count")) local textureStr = "TEXTURE = " .. (system.getInfo("textureMemoryUsed") / (1024 \* 1024)) print(memUsageStr, textureStr) end timer.performWithDelay(1000, checkMemory, 0)
And when running the following code:
local bounceUp local rect = display.newRect(0, 0, 20, 20) local transitionTimer = timer.performWithDelay(100, \_bounceUp) local function bounceOnComplete() transitionTimer = timer.performWithDelay(3000, bounceUp) end local function bounceDown() transition.to(rect, { time=300, y=0, transition=easing.inCirc, onComplete=bounceOnComplete }) end bounceUp = function() --timer.cancel(transitionTimer) -- I tried to add this line, but it did not help transitionTimer = nil collectgarbage() transition.to(rect, { time=300, y=-50, transition=easing.outCirc, onComplete=bounceDown }) end
I see the memory count keeps rising up.
It increments significantly, then decrements a little bit, over and over, so overall the usage is growing continuously.
It makes sense for the memory to rise, since I keep creating new timers, but it should also free the same amount when these timers end.
Also, I tried the following approach to see if it helps:
local bounceUp local rect = display.newRect(0, 0, 20, 20) local transitionTimer = timer.performWithDelay(3000, \_bounceUp, 0) local function bounceDown() transition.to(rect, { time=300, y=0, transition=easing.inCirc }) end bounceUp = function() transition.to(rect, { time=300, y=-50, transition=easing.outCirc, onComplete=bounceDown }) end
But to my surprise, the memory usage stayed the same.
It keeps creeping up, no matter what.
What is going on?
Why isn’t the memory released after the timer ends?