lua-users home
lua-l archive

Re: Incremental garbage collection ?

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


> What happens is that periodically the PLC scan time jumps from 2-3
> milliseconds to 32 milliseconds which is unacceptable for us. We believe
> that it is the garbage collection that is kicking in [...]
Well, the first step is to be sure whether the problem is really GC. You
can set a GC tagmethod for tag nil:
 lua_pushcfunction(L, f);
 lua_settagmethod(L, LUA_TNIL, "gc");
After this code, function 'f' will be called everytime Lua does a GC 
cicle. 
If the problem is really GC, I don't think it would be easy to implement an 
incremental garbage collector in Lua. But maybe you can solve your problem 
in another way. 
The API of Lua 4.0 gives you a good control over Lua GC. You can stop it,
force it, and check its internal state. So, if your application has some
"idle" time, you can use it to do a GC, and then you stop the GC during
the critical parts of your system. Something like this (untested code...):
 /* busy time: freeze GC */
 limit = lua_getgcthreshold(L); /* previous limit */
 lua_setgcthreshold(L, 1000000); /* set limit to 1G */
 /* idle time; check GC */
 /* using more than 0.8 of the limit? */
 if (lua_getgccount(L)/8 >= limit/10)
 lua_setgcthreshold(L, 0); /* force a GC cicle */
You can also use some fixed value for 'limit', if you have an estimation
of how much memory Lua can use.
-- Roberto

AltStyle によって変換されたページ (->オリジナル) /