I have a scroll view into which I insert a number of images. I re-use the variable name for the image.
function loadScroll()
local scrollImage = display.newImage(“Something.png”)
myScroll:insert(scrollImage)
.
.
.
scrollImage = display.newImage(“SomethingElse.png”)
myScroll:insert(scrollImage)
etc.
end
I’m thinking this doesn’t make all but the last image inaccessible and therefore vulnerable to garbage collection because myScroll, which is global, still references them even though scrollImage no longer does. And I don’t have to worry about removing all those images because when I remove myScroll that link is dissolved and that garbage will be collected.
To confirm this and to check for memory leaks I’ve put this temporarily in the program. I don’t know how often garbage collection occurs, so I thought I would just force it every three seconds.
function memCheck()
collectgarbage()
print(system.getInfo(“textureMemoryUsed”))
end
timer.performWithDelay(3000,memCheck,0)
Then I run the app and repeatedly instantiate myScroll, insert the images, browse it a while, and then remove it and set it to nil. I assume that if all but one of the images were inaccessible collectgarbage() would free that memory and those images would disappear. That doesn’t happen; I can browse them for a seemingly indefinite time. Meanwhile I don’t see memory consumption shooting up with repeated passes, although the images consume several MB. Are my assumptions correct? And is this the right way to catch memory leaks?