Hope you can help me with this guys,
We are planning on using lua for equipment control and for that purpose out
PLC engine has been modified so that it can call lua subroutines.
This works well and seems to be fast enough for our needs and makes a lot
of our controls much simpler, both to write and to maintain (compared to
What happens is that periodically the PLC scan time jumps from 2-3
milliseconds to 32 milliseconds which is unacceptable for us. We believe
is the garbage collection that is kicking in and taking up all this time.
Is this something that we have to try to live with or is it possible to do
of incremental garbage collection through a low priority taks that does
nothing but do garbage collection. Is the time it takes to do garbage
or would it help to do garbege collection more frequently ?
> 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_settagmethod(L, LUA_TNIL, "gc");
After this code, function 'f' will be called everytime Lua does a GC
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.