Link to home
Start Free TrialLog in
Avatar of jasonbb
jasonbbFlag for Australia

asked on

in Corona and Lua - how do I completely remove a display object (an image) from the screen and also from memory

I am trying to fix my memory leak in Lua.
To test, I am creating 100,000 images on screen and recording them.
Then I remove them from the display. This works.
The problem is that the memory used never reduces back to that before the creation.
Here is my code:

local _storeContents = {}
local function OnDebugTest( event )
    for i = 1, 100000, 1 do
        img = display.newImage( "planet1.png", math.random()*400, math.random()*400-200 )
        _group:insert(img)
        table.insert(_storeContents,img)
    end

    for a,b in pairs(_storeContents) do
        display.remove(b)
    end

    _storeContents = {}
end

I am using gcinfo() to get the memory usage.
What do I need to do to fix the memory leak?
Avatar of jasonbb
jasonbb
Flag of Australia image

ASKER

I think the code is working and is releasing the memory.
I made this change to the code:
    for a,b in pairs(_storeContents) do
        display.remove(b)
        _storeContents [a] = nil
    end
and I use this later, called from a button:
    collectgarbage()
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.