lua-users home
lua-l archive

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


On Wed, 17 Dec 2014 17:31:49 -0800
Tim Hill <drtimhill@gmail.com> wrote:

> No you won’t be leaking memory (unless you have a bug in your own C
> code!). My point about the extra garbage collect was purely addressed
> at housekeeping. Lua will eventually perform a GC and recover memory
> without any help from you. However, imagine that your script
> allocates a large table during processing, then sets this table to
> nil just before one of your 100ms “ticks” finishes (and the script
> ends). This table is of course a candidate for collection, and will
> be collected _at some point_, but it’s quite possible that this will
> not happen until the next (or later) 100ms tick (remember Lua has no
> autonomous GC thread, so it can only collect while a script is
> running or (in some cases) when you call the Lua API).
> 
> What this means is that garbage left over from one of your script
> 100ms “ticks” might not be collected until the next “tick”. If you ok
> with that, great, but if you are concerned about that memory not
> being released for use (say) by the C code, then you should call the
> lua_gc() API to trigger a collect when your script returns.

Okay. Thanks, this is very helpful.

Yesterday, I run my code with periodic call of lua_gc(LUA_GCCOUNT) at it
turned out it was allocating about 1-3k after each "tick". I found out that I
called lua_getglobal("main") (to be able to run the 'main' function), and I
added a table (as argument) and its elements to the stack. I realised that I
was only poping the return values of my Lua function. So there must be so many
things left on the stack.

So I ended up calling lua_settop(L, 0) and then the lua_gc(LUA_GCCOLLECT).