Why does this short piece of code use up memory and does not release

Code is showing as below. What I am doing is create lots of group and then remove all of them. I expect they shouldn’t take up memory. However, the result isn’t like that. Am I missing anything here?

for i = 1, 100000 do

 local fff = display.newGroup()

 fff:removeSelf()

 fff = nil

end

i = nil

collectgarbage(“collect”)

timer.performWithDelay(1000, function()

 print(collectgarbage(“count”))

end, 0)

put another collectgarbage(“collect”) inside your timer callback before printing the count – i think all you’re seeing is some residual junk from the timer callback itself that isn’t getting collected immediately.

(that is, i don’t think you’re seeing residual from the display groups – in fact, you can probably comment out that entire first section and still see the residual timer junk unless you add that additional collect inside the callback)

Try this:

print(collectgarbage(“count”))
for i = 1, 100000 do
 local fff = display.newGroup()
 fff:removeSelf()
 fff = nil
 collectgarbage(“collect”)
end
i = nil
collectgarbage(“collect”)

print(collectgarbage(“count”))
timer.performWithDelay(1000, function()
 collectgarbage(“collect”)
 print(collectgarbage(“count”))
end, 0)

The timer executing creates some memory and since you never reclaim it, it appears to go up until GC runs again. 

put another collectgarbage(“collect”) inside your timer callback before printing the count – i think all you’re seeing is some residual junk from the timer callback itself that isn’t getting collected immediately.

(that is, i don’t think you’re seeing residual from the display groups – in fact, you can probably comment out that entire first section and still see the residual timer junk unless you add that additional collect inside the callback)

Try this:

print(collectgarbage(“count”))
for i = 1, 100000 do
 local fff = display.newGroup()
 fff:removeSelf()
 fff = nil
 collectgarbage(“collect”)
end
i = nil
collectgarbage(“collect”)

print(collectgarbage(“count”))
timer.performWithDelay(1000, function()
 collectgarbage(“collect”)
 print(collectgarbage(“count”))
end, 0)

The timer executing creates some memory and since you never reclaim it, it appears to go up until GC runs again.