lua-users home
lua-l archive

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

From the reference manual (, page 27 (33 if your pdf reader doesn't understand page numbers):

Lua uses two numbers to control its garbage collection: the count and the threshold (see §2.9). The first counts the amount of memory in use by Lua; when the count reaches the threshold, Lua runs its garbage collector. After the collection, the count is updated and the threshold is set to twice the count value.

You can access the current values of these two numbers through the following functions:
   int lua_getgccount (lua_State *L);
   int lua_getgcthreshold (lua_State *L);

Both return their respective values in Kbytes.

You can change the threshold value with void lua_setgcthreshold (lua_State *L, int newthreshold); Again, the newthreshold value is given in Kbytes. When you call this function, Lua sets the new threshold and checks it against the byte counter. If the new threshold is less than the byte counter, then Lua immediately runs the garbage collector. In particular lua_setgcthreshold(L,0) forces a garbage collection. After the collection, a new threshold is set according to the previous rule.

Note: I believe this interface will change with Lua 5.1.

On 7-Aug-05, at 11:20 AM, Uli Kusterer wrote:

Hi, it's me again.

I've found luaC_collectgarbage() in lgc.h for explicitly triggering the garbage collector. However, if I understand this correctly, we're only supposed to use the "public" Lua API, i.e. the stuff in lua.h and lualib.h (and lauxlib.h). Is it okay to use this GC call, or am I asking for trouble and making it harder to update to future versions of Lua?

Since my app has several distinct phases, and thus certain clear breaks at which I *know* there will be lots of objects in need of being reclaimed, I'd like to be able to explicitly trigger the GC. People will be waiting between these phases anyway, and thus won't notice the time the GC could take. This function seems to work, but since it's not part of the public API, I fear I may have overlooked some more obvious means of achieving the same. Any clues will be appreciated.

-- M. Uli Kusterer