Lua memory question

Hi,

I’m curious about a simple question: Does Lua (or Corona SDK) free all the memory consumed by the “local” variables that I created when the class (or function) finishes it execution?

Here is my code: (I’m using Director class by the way and this is my loading screen)

local levelElements = {} local roomBackgrounds = {} local levelObjects = {} local inventoryItems = {} local popupObjects = {} local popupAreas = {} local levelMusic = levelData.music levelElements["levelMusic"] = levelMusic for key, data in pairs (levelData.backgrounds) do local background background = display.newImageRect(data, display.contentWidth, display.contentHeight) background.id = key background.x = display.contentCenterX background.y = display.contentCenterY background.isVisible = false roomBackgrounds[background.id] = background end levelElements["roomBackgrounds"] = roomBackgrounds local bag = display.newImageRect(commonData.bag, pngLib.getPngInfo(commonData.bag).width, pngLib.getPngInfo(commonData.bag).height) bag.isVisible = false levelObjects["bag"] = bag levelElements["levelObjects"] = levelObjects director:changeScene({levelElements}, "scripts.levels.level" .. \_G.currentLevel, "crossfade")

Do I need to free my memory consumed by tables other than levelElements or Lua(or Corona SDK) does that by itself?

Hi @jarbull,

Unless these local variables are being “held onto” somehow, they should be collected by the Lua garbage collector automatically. The only thing I see in your code is the reference to “levelData.music”, which implies that you’re playing that music somewhere. You probably already know this, but ensure that you dispose of that music when you’re finished with it.

Brent

Hi Brent,

Sorry that I forgot to write my name because I thought I was posting with my personal account :slight_smile:

The levelData.music actually just holds a string so I think there is no problem about memory allocation here.

Thank you for your time and answer :slight_smile:

Serkan

Hi @jarbull,

Unless these local variables are being “held onto” somehow, they should be collected by the Lua garbage collector automatically. The only thing I see in your code is the reference to “levelData.music”, which implies that you’re playing that music somewhere. You probably already know this, but ensure that you dispose of that music when you’re finished with it.

Brent

Hi Brent,

Sorry that I forgot to write my name because I thought I was posting with my personal account :slight_smile:

The levelData.music actually just holds a string so I think there is no problem about memory allocation here.

Thank you for your time and answer :slight_smile:

Serkan