Lua garbage collection


Lua uses automatic memory management. This means that you don't have to worry about how to allocate the memory required for newly created objects, nor do you need to worry about how to release the memory occupied by the objects after they are no longer used.

Lua runs a garbage collector to collect all dead objects (that is, objects that can no longer be accessed in Lua) to complete the work of automatic memory management . All memory used in Lua, such as strings, tables, user data, functions, threads, internal structures, etc., are subject to automatic management.

Lua implements an incremental mark-scan collector. It uses these two numbers to control the garbage collection cycle: garbage collector interval rate and garbage collector step rate. Both numbers are expressed in percent units (for example: a value of 100 represents 1 internally).

The garbage collector interval rate controls how long the collector needs to wait before starting a new cycle. Increasing this value will make the collector less aggressive. When this value is smaller than 100, the collector will not wait before starting a new cycle. Setting this value to 200 will cause the collector to wait until the total memory usage reaches twice the previous value before starting a new cycle.

The garbage collector step ratio controls the ratio of the collector's operating speed relative 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 less than 100, otherwise the collector will work too slowly and never complete a cycle. The default value is 200 , which means the collector works "twice" as fast as memory allocation.

If you set the step ratio to a very large number (10% larger than the number of bytes your program may use), the collector behaves like a stop-the-world collection device. Then if you set the pause rate to 200, the collector behaves the same as in previous versions of Lua: every time the memory used by Lua doubles, a full collection is done.


Garbage collector function

Lua provides the following functioncollectgarbage ([opt [, arg]]) used to control automatic memory management:

  • collectgarbage("collect"): Do a complete garbage collection cycle. It provides a different set of functions through the parameter opt:

  • collectgarbage("count"): Returns the total memory used by Lua in K bytes number. This value has a fractional part, so just multiply by 1024 to get the exact number of bytes used by Lua (barring overflow).

  • collectgarbage("restart"): Restart the automatic operation of the garbage collector.

  • collectgarbage("setpause"): Set arg to the collector's pause rate (see §2.5). Returns the previous value of interval rate.

  • collectgarbage("setstepmul"): Returns the previous value of step magnification.

  • collectgarbage("step"): Run the garbage collector in one step. The step "size" is controlled by arg. When 0 is passed in, the collector steps by (indivisible) one step. Passing in a non-zero value, the collector does the equivalent of Lua allocating these many (K bytes) of memory. Returns true if the collector ends a loop.

  • collectgarbage("stop"): Stop the garbage collector. The collector will only run for explicit calls before calling restart.

The following demonstrates a simple garbage collection example:

mytable = {"apple", "orange", "banana"}

print(collectgarbage("count"))

mytable = nil

print(collectgarbage("count"))

print(collectgarbage("collect"))

print(collectgarbage("count"))

Execute the above program and the output results are as follows (note the changes in memory usage):

20.9560546875
20.9853515625
0
19.4111328125