Object delete memory question...

When using a display group I want to create some objects in a function on the fly like for example:

myGroup=display.newGroup() makeobj = function()     local obj=display.newImage(...)     myGroup:insert(obj)     obj=nil end

Is this okay when I call later at scene change:

display.remove(myGroup) myGroup=nil

will the object generated inside the function then completely removed from memory?

Thats the beauty of using display groups.

When you remove the group, all children are removed as well.

I don’t think you need to do obj=nil in the creation loop either.

When you later nil the display group, all is gone.

I do a lot of this in my current project and have run thousands of children while watching ram use, and i havent found any leaks.

When that memory is freed is up to Lua.  Also, it may never be released.

Why never? 

Because Lua memory management is smart. 

As your program runs, your memory needs increase.  To avoid constantly freeing and de-allocating memory, Lua  allocates memory in blocks.  It then self-manages this block.

In addition to using blocks,  Lua prefers to hold onto a block even if it becomes completely unused later.  It does this to avoid turning around and reallocating the block moments later.

It does de-allocate blocks, but not immediately unless the OS is putting the app under pressure to do so, or unless you manually clear garbage (bad idea for performance).

http://luatut.com/collectgarbage.html 

Thats the beauty of using display groups.

When you remove the group, all children are removed as well.

I don’t think you need to do obj=nil in the creation loop either.

When you later nil the display group, all is gone.

I do a lot of this in my current project and have run thousands of children while watching ram use, and i havent found any leaks.

When that memory is freed is up to Lua.  Also, it may never be released.

Why never? 

Because Lua memory management is smart. 

As your program runs, your memory needs increase.  To avoid constantly freeing and de-allocating memory, Lua  allocates memory in blocks.  It then self-manages this block.

In addition to using blocks,  Lua prefers to hold onto a block even if it becomes completely unused later.  It does this to avoid turning around and reallocating the block moments later.

It does de-allocate blocks, but not immediately unless the OS is putting the app under pressure to do so, or unless you manually clear garbage (bad idea for performance).

http://luatut.com/collectgarbage.html