VersionNotice: This article relates to an earlier version of Lua. Incremental garbage collection is available as of Lua 5.1.
Older versions of Lua (prior to 5.1) are not well-suited for the main loops of real-time programs. The reason isn't that Lua is slow--in fact it probably has the fastest virtual machine of any scripting language. The problem is the mark-and-sweep garbage collection (GC), which requires a concentrated chunk of CPU time proportional to the number of objects allocated. It was useful in 5.1 to replace the mark-and-sweep collector with a real-time incremental system (see paper Real-Time Non-Copying Garbage Collection [1]). In older version of Lua, there are still several ways to make good use of Lua in real-time programs:
- Use Lua only at init time. Use Lua scripts to initialize C structures, then close or discontinue use of the Lua context.
- Use Lua in main loop but do not allocate objects. With careful programming it's possible to eliminate object allocation in the main loop, in which case the GC doesn't need to run.
- Keep amount of allocated objects small and run the GC after every iteration of the main loop. For a small number of objects this may be reasonable. Note that the standard libraries are included in this count so it's best to strip unused functions.
See also: OptimisationTips, GarbageCollection/GarbageCollectionInRealTimeGames, ProfilingLuaCode
RecentChanges · preferences
edit · history
Last edited October 25, 2008 4:53 pm GMT (diff)