Loading the same image more than once, more memory?

If I’m loading the same image more than once does Coron recognize this and only reserve memory for the image once, or not. 

For example, I have a couple modules that each use sprites from the same sprite sheet. Should each module load the sprite sheet image, or should should I load the image outside and pass a reference to each module? 

i do this in my main lua and it does only reserve the memory once. 
but i havent tried it with modules yet.

it should be easy to find out with this little snippet placed at the bottom of your main.lua (or of your scene)

[lua]

local dbug_monitorMem = function()

    

    collectgarbage(“collect”)

    print( "\nMemUsage: " … collectgarbage(“count”) )

    local textMem = system.getInfo( “textureMemoryUsed” ) / 1048576 

    print( “TexMem: " … textMem …” mb")

    

end 

local memTimer = timer.performWithDelay(1000, dbug_monitorMem, -1)

[/lua]

sorry for not having a clear yes or no answer to you,
but i thought its better to share what i experienced my own than leaving this thread unanswered.
i hope it was of some help nevertheless,
cheers,
michael

Hi @soggybag,

In terms of texture memory (which is very important), loading the same image multiple times will not increase that. It’s in the cache at that point, loaded and ready to go.

But of course, putting the same image on the screen 1000 times will take more Lua/general memory, as expected, because they are all Corona display objects.

Best regards,

Brent

i do this in my main lua and it does only reserve the memory once. 
but i havent tried it with modules yet.

it should be easy to find out with this little snippet placed at the bottom of your main.lua (or of your scene)

[lua]

local dbug_monitorMem = function()

    

    collectgarbage(“collect”)

    print( "\nMemUsage: " … collectgarbage(“count”) )

    local textMem = system.getInfo( “textureMemoryUsed” ) / 1048576 

    print( “TexMem: " … textMem …” mb")

    

end 

local memTimer = timer.performWithDelay(1000, dbug_monitorMem, -1)

[/lua]

sorry for not having a clear yes or no answer to you,
but i thought its better to share what i experienced my own than leaving this thread unanswered.
i hope it was of some help nevertheless,
cheers,
michael

Hi @soggybag,

In terms of texture memory (which is very important), loading the same image multiple times will not increase that. It’s in the cache at that point, loaded and ready to go.

But of course, putting the same image on the screen 1000 times will take more Lua/general memory, as expected, because they are all Corona display objects.

Best regards,

Brent

Hi Brent,

I see in this thread (and in docs) about images being cached. But it seems like the images are still being garbage collected once removed. E.g. display.remove(img) removes image from cache. I don’t know this is so, but it definitely seems like it from my testing. This makes sense since I don’t really want every image ever loaded by the app hanging around taking up texture memory. But since the docs don’t state this is the case I wanted to double check.

The docs say “All loaded images are cached. To save texture memory the image in the cache memory is used when it detects an image with the same file name is displayed. This means that loading the same image multiple times doesn’t increase the amount of texture memory used on the device.”

Am I correct that the documentation would be more accurate if I added “However, once the image is garbage collected it is no longer in cache.”

The particular reason I ask is because I need to preload batches of images. It seems that just calling display.newImage() isn’t enough. Rather, I need to store a reference to them to prevent garbage collection.

Any clarity is much appreciated!

 

JB

Hi JB,

The image is actually cleared from the cache once it’s removed from the display, not necessarily garbage collected. You can test this easily by putting some images on the screen, watching the texture memory using the common “checkMem” function (shown below), and then remove the images from the display after a certain amount of time. The texture memory drops once the images are cleared from the display, even if you don’t take the full (and expected) step of nil’ing out the reference.

[lua]

local function checkMem()

    collectgarbage(“collect”)

    local memUsage_str = string.format( “MEMORY = %.3f KB”, collectgarbage( “count” ) )

    print( memUsage_str, "TEXTURE = "…(system.getInfo(“textureMemoryUsed”)/1048576) )

end

timer.performWithDelay( 1000, checkMem, 0 )

local im = display.newImage(“image1.png”)

local im2 = display.newImage(“image2.png”)

local function rem()

    display.remove(im)

    display.remove(im2)

end

timer.performWithDelay( 4500, rem )

[/lua]

Thanks for clearing that up  Brett!

Hi Brent,

I see in this thread (and in docs) about images being cached. But it seems like the images are still being garbage collected once removed. E.g. display.remove(img) removes image from cache. I don’t know this is so, but it definitely seems like it from my testing. This makes sense since I don’t really want every image ever loaded by the app hanging around taking up texture memory. But since the docs don’t state this is the case I wanted to double check.

The docs say “All loaded images are cached. To save texture memory the image in the cache memory is used when it detects an image with the same file name is displayed. This means that loading the same image multiple times doesn’t increase the amount of texture memory used on the device.”

Am I correct that the documentation would be more accurate if I added “However, once the image is garbage collected it is no longer in cache.”

The particular reason I ask is because I need to preload batches of images. It seems that just calling display.newImage() isn’t enough. Rather, I need to store a reference to them to prevent garbage collection.

Any clarity is much appreciated!

 

JB

Hi JB,

The image is actually cleared from the cache once it’s removed from the display, not necessarily garbage collected. You can test this easily by putting some images on the screen, watching the texture memory using the common “checkMem” function (shown below), and then remove the images from the display after a certain amount of time. The texture memory drops once the images are cleared from the display, even if you don’t take the full (and expected) step of nil’ing out the reference.

[lua]

local function checkMem()

    collectgarbage(“collect”)

    local memUsage_str = string.format( “MEMORY = %.3f KB”, collectgarbage( “count” ) )

    print( memUsage_str, "TEXTURE = "…(system.getInfo(“textureMemoryUsed”)/1048576) )

end

timer.performWithDelay( 1000, checkMem, 0 )

local im = display.newImage(“image1.png”)

local im2 = display.newImage(“image2.png”)

local function rem()

    display.remove(im)

    display.remove(im2)

end

timer.performWithDelay( 4500, rem )

[/lua]

Thanks for clearing that up  Brett!