I understand that Corona does seem to cache and reuse images loaded from the same path in some way even if it was removed from the viewport. So the question is: What happens if I use all different images!?
To test this I wrote some simple test code which will duplicate the “test.jpg” you place into your project directory 300 times into the documents folder of the app and shows every single copy for 1 second and remove it afterward.
-- creating copies of an image file to test memory behaviour
-- reading source file from resources
io.output():setvbuf('no')
-- read imagedate for later
local fname=system.pathForFile("test.jpg", system.ResourceDirectory)
local fh=io.open(fname,'rb')
local imagedata=fh:read('\*a')
io.close(fh)
function createCopy(i)
-- creating a copy if not existing
local iname="iapp\_test5\_"..i..".jpg"
local ipath=system.pathForFile(iname, system.DocumentsDirectory)
local fh=io.open(ipath,'rb')
if fh then
-- file existing
io.close(fh)
else
print("Create image "..i)
local fh=io.open(ipath,'wb')
fh:write(imagedata)
io.close(fh)
end
return iname
end
do
local frame=0
local image=1
local max=300
function render(event)
frame=frame+1
if frame == 1 then
local iname=createCopy(image)
print("Adding an image "..iname)
local r=display.newImage(iname,system.DocumentsDirectory)
image=image+1
if image \> max then
image=1
end
elseif frame == 30 then
print("Removing the image in stage")
local s=display:getCurrentStage()
s:remove(s[1])
--local x=s:remove(s[1])
--x=nil
frame=0
end
end
Runtime:addEventListener( "enterFrame", render )
end
I am using the device console and instruments to trac the program running on my iPhone.
It WILL eat a lot of memory while running… I had it collect about 32 MB of memory down back to 6 at about picture 30
After 76 pictures the console started to print low memory warnings and “mail” was asking to exit
(which launchd tried to restored immediately)
At 83 it asked “telephone” to quit… and short afterwards the test app was killed
Afterwards the test applicaton could not be started again!!!
I had to reset the iPhone to get it running again!
INTERESTING: Sometimes! It collects after the first memory warning… sometimes not. My very first run of this program was shrinking memory usage after about 30 pics back to 6 MB from 49 MB. Later runs of the same program did not do it! I wonder if the treshold Corona may use for its texture/globject cache is just to low to generate a stable behaviour?
So there is something wrong with Corona in this aspect!
P.S.: The commented out code is just a reference to another thread here… will not change anything… so don’t take that serious :)) [import]uid: 6928 topic_id: 1251 reply_id: 301251[/import]
[import]uid: 6928 topic_id: 1251 reply_id: 3294[/import] </avcontroller:></avcontroller:>
… Even thinking it could, would mean Lua is totally brocken in respekt of reference counting. And Lua is pretty good with that.
… the Lua memory for a table can’t be actively nil’ed or free’d … stop telling that you can “nil” an object. It is not true. You can remove all it’s references by setting their variables to nil or by letting them leave the scope.