lua-users home
lua-l archive

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


Andreas Stenius said:
> Hi,
>
> I'm running lua (5.0.2) on a embedded device (e.g. limited amount of RAM
> available).
> When does the gc kick in during lua code execution?

When the gc threshold is hit, but see below.
>
> Here's what I've got:
> A bunch of lua code is running and I have about 100k RAM left (using 200k,
> as reported from gcinfo), I keep the gc threshold at 280k, which keeps the
> app running under normal op. However, when I loop over a function that
> uses
> quite a bit of RAM it bails out the second time around with: not enough
> memory.

gcinfo() reports the amount of memory being *used* by Lua. It does not
report the amount of RAM being used to provide that memory to Lua. In
particular, it does not report either direct malloc overhead or unusable
memory (blocks too small to service an allocation request). There is no
portable interface to get this information, and many implementations of
malloc provide no usable interface at all.

Lua's gc threshold is similarly defined in terms of the actual memory used
(without considering overhead or fragmentation) so if the threshold is
280k, the gc will be called when 280k is actually in use by Lua. The
amount of memory being used by the memory allocation system to provide
that 280k might be quite a bit larger.

>
> I call collectgarbage before each iteration, and the processing each times
> is
> as good as identical.

Also, make sure to reset the threshold after collectgarbage().