Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua garbage collection


May 12, 2021 Lua


Table of contents


Lua garbage collection

Lua uses automatic memory management. This means that you don't have to worry about how the memory required for newly created objects is allocated, or how to free up the memory they use when they are no longer in use.

Lua runs a garbage collector to collect all dead objects (that is, objects that are no longer accessible in Lua) for automated memory management. A ll memory used in Lua, such as strings, tables, user data, functions, threads, internal structures, and so on, is subject to automatic management.

Lua implements an incremental tag-scan collector. I t uses these two numbers to control the garbage collection cycle: the garbage collector interval rate and the garbage collector step multiply rate. B oth numbers are in percentages (for example, a value of 100 represents 1 internally).

The garbage collector interval rate controls how long the collector needs to wait before opening a new cycle. I ncreasing this value reduces the collector's motivation. W hen this value is smaller than 100, the collector does not wait until a new loop is opened. S etting this value to 200 allows the collector to wait until the total memory usage is twice as high as before to start a new loop.

The garbage collector step multiply controls the rate at which the collector operates relative to the memory allocation speed. I ncreasing this value not only makes the collector more active, but also increases the length of each incremental step. D on't set this value to less than 100, then the collector will work too slowly to complete a loop. T he default is 200, which means that the collector is working at "twice" the speed of memory allocation.

If you set the step multiply to a very large number (10% 10% bigger than the number of bytes your program might use), the collector behaves like a stop-the-world collector. Then if you set the interval rate to 200, the collector behaves the same as in previous versions of Lua: every time Lua doubles the memory used, it does a complete collection.


The garbage collector function

Lua provides the following function, collectgarbage ('opt', arg') to control automatic memory management:

  • collectgarbage ("collect"): Do a complete garbage collection cycle. With the parameter opt it provides a different set of features:

  • Collectgarbage ("count"): Returns the total amount of memory used by Lua in K-bytes. This value has a small number, so you only need to multiply 1024 to get the exact number of bytes that Lua uses (unless it overflows).

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

  • Collectgarbage ("setpause"): Set arg as the collector's interval rate (see s2.5). Returns the previous value of the interval rate.

  • Collectgarbage ("setstepmul"): Returns the previous value of the step multiply.

  • Collectgarbage ("step"): Run the garbage collector in one step. S tep Size is controlled by arg. W hen 0 is passed in, the collector steps (indivisible) one step. P assing in a non-0 value, the collector collects the equivalent of Lua allocating these many (K-bytes) of memory. True if the collector ends a loop.

  • collectgarbage ("stop"): Stop the garbage collector from running. Between calls to restart, the collector will only run because of explicit calls.

Here's a simple example of garbage collection:

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

print(collectgarbage("count"))

mytable = nil

print(collectgarbage("count"))

print(collectgarbage("collect"))

print(collectgarbage("count"))

To execute the above program, the output is as follows (note the change in memory usage):

20.9560546875
20.9853515625
0
19.4111328125