[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to collect as much as possible with collectgarbage() ?
- From: Roberto Ierusalimschy <roberto@...>
- Date: Thu, 9 Jul 2009 13:29:57 -0300
> IIRC the number of cycles necessary depends on the depth of the data
> to collect. For example
> t={}
> t=nil
> will require one cycle, while
> t = { t = { t = { t = { t = { t = {} } } } } }
> will require 6 cycles.
That is not true, unless the program uses weak tables. Otherwise a table
like the one above should be collected in one cycle.
There are three things that may "delay" the release of all available
memory. One is weak tables. Another is finalizers: a userdata with
a __gc metamethod cannot be collected in the same cycle that runs
its finalizer. The third is some internal arrays, such as stacks and
buffers. When they are too big, the collector shrinks them by some fixed
factor (e.g., it may halve the original size). So, if they are really
big, it may take some cycles for them to shrink to a size that the
collector will not shrink anymore.
Probably what is happening in that program is this: when Lua reads
that huge string, an internal buffer grows to accomodate that string.
After that, it takes some collections for the buffer to resize to
a reasonable size again.
-- Roberto