Emergency Garbage Collector

lua-users home

This page is about creating an emergency garbage collector patch for Lua 5.1.4. Please note that LuaFiveTwo has an emergency garbage collector listed as one of the features that will be included. Work on this patch is being done separately from the emergency garbage collector that might be included in LuaFiveTwo.

The Emergency GC patch makes it safe to call the Lua garbage collector after a memory allocation has failed. This allows the garbage collector to free some memory so that the failed allocation can be retried. The patch also add support for setting a limit on how much memory Lua scripts can allocate.


[Download emergency gc patch for (5.1.4) release 6]

[Program for stress-testing the emergency garbage collection.]

Extra memory optimization features.

These are included in the Emergency GC patch. They can be used without the emergency gc patch.

Notes about how the Lua garbage collector works

Disclaimer: This is the first time I have worked on a garbage collector so some of this may be incorrect. Corrections/cleanups are welcome. --RobertGabrielJakabosky

"While working on this patch I had to learn how the garbage collector in Lua works. I am writing this to help me later if I need to fix more bugs with the collector and I hope this info can help other people who are interested in how the Lua garbage collector works." --RobertGabrielJakabosky

Simple description

The Lua garbage collector is a mark & sweep collector. The collector has two major phases mark & sweep that it runs each collection cycle. During the mark phases the collector traverse the Lua stack and into tables to mark values it finds as live. Next the sweep phases will walk a list of all collectible values and free all dead values it finds.

Detailed description

All collectible type objects have a 'marked' bit field. The bits are defined as (copied from header "lgc.h"): The garbage collector keeps track of a current white (type 0 or 1) and objects with the other white are dead objects that can be collected during the sweep states.

An object's color is defined by which of the first 3 bits (0, 1, 2) are set:

Garbage collector states (each collection cycle passes through these states in this order):


See Also

RecentChanges · preferences
edit · history
Last edited December 8, 2010 6:05 am GMT (diff)