Lua Garbage Collection
Lua uses automatic memory management. This means you don't have to worry about how the memory needed for newly created objects is allocated, nor about how to release the memory occupied by objects when they are no longer used.
Lua runs agarbage collectorto collect alldead objects (i.e., objects that can no longer be accessed in Lua) to accomplish automatic memory management. All memory used in Lua, such as strings, tables, userdata, functions, threads, internal structures, etc., is subject to automatic management.
Lua implements an incremental mark-and-sweep collector. It uses two numbers to control the garbage collection cycle: the garbage collector pause and the garbage collector step multiplier. Both numbers are expressed as percentages (for example, a value of 100 is represented internally as 1).
The garbage collector pause controls how long the collector waits before starting a new cycle. Increasing this value makes the collector less aggressive. When this value is less than 100, the collector does not wait before starting a new cycle. Setting this value to 200 makes the collector wait until total memory usage reaches twice the previous amount before starting a new cycle.
The garbage collector step multiplier controls the ratio of the collector's operating speed to the memory allocation speed. Increasing this value not only makes the collector more aggressive, but also increases the length of each incremental step. Do not set this value below 100, because then the collector would work too slowly to ever complete a cycle. The default value is 200, which means the collector works at "twice" the speed of memory allocation.
If you set the step multiplier to a very large number (10% larger than the number of bytes your program might use), the collector behaves like a stop-the-world collector. If you then set the pause to 200, the collector behaves like older versions of Lua: a full collection is performed each time the memory used by Lua doubles.
Garbage collector functions
Lua provides the following function **collectgarbage ([opt [, arg]])** to control automatic memory management:
-
collectgarbage("collect"): Perform a complete garbage collection cycle. Through the opt parameter it provides a set of different functions:
-
collectgarbage("count"): Return the total memory used by Lua in K bytes. This value has a fractional part, so you only need to multiply by 1024 to get the exact number of bytes used by Lua (unless overflow occurs).
-
collectgarbage("restart"): Restart the automatic operation of the garbage collector.
-
collectgarbage("setpause"): Set arg as the collector's pause. Returns the previous value of the pause.
-
collectgarbage("setstepmul"): Returns the previous value of the step multiplier.
-
collectgarbage("step"): Run the garbage collector one step. The "size" of the step is controlled by arg. When passing 0, the collector advances one (indivisible) step. When passing a non-zero value, the collector performs work equivalent to Lua allocating that much more (K bytes) of memory. If the collector finishes a cycle, it returns true.
-
collectgarbage("stop"): Stop the garbage collector. Until restart is called, the collector only runs due to explicit calls.
The following demonstrates a simple garbage collection example:
Example
mytable
=
{
"apple"
,
"orange"
,
"banana"
}
(
collectgarbage
(
"count"
)
)
mytable
=
nil
(
collectgarbage
(
"count"
)
)
(
collectgarbage
(
"collect"
)
)
(
collectgarbage
(
"count"
)
)
After executing the above program, the output is as follows (note the changes in memory usage):
20.9560546875
20.9853515625
0
19.4111328125