我不确定为什么下面的示例代码没有释放所有计时器内存。它从大约 133kb 开始,如果您点击屏幕,它会创建 10,000 个计时器。运行后,应将其清理干净。但是,在删除所有计时器后,它会稳定在大约 389kb。
我错过了什么吗?
local timersFired = 0
local timers = {}
local maxTimers = 10000
Runtime:addEventListener("touch", function(e)
if(e.phase == "began") then
print("TIMERS CREATED")
timers = {}
for i=1,maxTimers do
table.insert(timers, timer.performWithDelay(3000, function(e)
timersFired = timersFired + 1
end, 1))
end
end
end)
Runtime:addEventListener("enterFrame", function(e)
if(timersFired == maxTimers) then
print("KILLED TIMERS")
for i=1,maxTimers do
local aTimer = timers[i]
timer.cancel(aTimer)
timers[i] = nil
end
timers = nil
timersFired = 0
end
collectgarbage("collect")
print( "MemUsage: " .. collectgarbage("count") )
end)