lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Request for clarification about references to internal table elements
and
> garbage collection.
> 
> Consider the following:
> 
> ---
> 
> Example #1:
> 
>  outer = { "outer table"; inner = { "inner table" } }
>  outer = nil
> 
> Statement #1:
> 
>  Outer table and all it contains will eventually be GC'd.
> 
> ---
> 
> Example #2:
> 
>  outer = { "outer table"; inner = { "inner table" } }
>  inner = outer.inner
>  outer = nil
> 
> Statement #2:
> 
>  Though there is no longer a direct reference to the outer table,
neither
> it
> nor
>  its' contents will be GC'd because there is still a reference to some
> part
> of
>  it.
> 
> ---
> 
> Example #3:
> 
>  outer = { "outer table"; inner = { "inner table" } }
>  outer.inner = nil
> 
> Statement #3:
> 
>  The inner table will eventually be GC'd but the outer table and all
of
> its
>  other contents will remain.
> 
> ---
> 
> Taking the examples into account, are the above statements correct?

Correct on all counts.

> = gcinfo()
42      83
> outer = {}
> outer[1] = string.rep("*",100000)
> = gcinfo()
164     327
> outer.inner = {}
> outer.inner[1] = string.rep("*",100001)
> inner = outer.inner
> = gcinfo()
262     522
> outer = nil
> collectgarbage()
> = gcinfo()
139     278

If you allocate big strings you can use gcinfo() and collectgarbage() to
test for leaks and collections.

http://lua-users.org/wiki/GarbageCollectionTutorial
http://lua-users.org/wiki/CoreFunctionsTutorial

Nick