English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Lua Garbage Collection

Lua uses automatic memory management. This means you don't have to worry about how to allocate memory for new objects, nor do you need to consider how to release the memory occupied by objects that are no longer in use.

Lua runs agarbage collectorto collect alldead objects (which is an object that cannot 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 marking-Scanning collector. It uses these two numbers to control the garbage collection cycle: the garbage collector intermittent rate and the garbage collector step multiplier. Both numbers are in percentage units (for example: the value 100 internally represents 1 ).

The garbage collector intermittent rate controls how long the collector needs to wait before starting a new loop. Increasing this value reduces the积极性 of the collector. When this value is compared to 100 when it is small, the collector does not wait before starting a new loop. Set this value to 200, which would make the collector wait until the total memory usage reaches twice the previous amount before starting a new loop.

The garbage collector step multiplier controls the rate at which the collector operates 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, in that case, the collector works too slowly to finish a loop forever. The default value is 200, this indicates that the collector works at twice the speed of memory allocation.

If you set the step multiplier to a very large number (larger than the number of bytes your program might use) 10%), the behavior of the collector is similar to a stop-the-world collector. Then if you set the intermittent rate to 200, the collector's behavior is the same as in past Lua versions: it performs a full collection each time the memory used by Lua doubles.

Garbage collector functions

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

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

  • collectgarbage("count"): Returns the total amount of memory used by Lua in K bytes. This value has a fractional part, so you only need to multiply it by 1024 can get the accurate number of bytes used by Lua (unless overflow).

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

  • collectgarbage("setpause"): Set arg to the collector's interval rate. Return the previous interval rate.

  • collectgarbage("setstepmul"): Return the previous value of the step multiplier.

  • collectgarbage("step"): Single-step run the garbage collector. The step size "size" is controlled by arg. When 0 is passed, the collector steps (irrevocably) one step. When a non-zero value is passed, the collector collects work equivalent to Lua allocating these many (K bytes) of memory. If the collector ends a loop, it will return true.

  • collectgarbage("stop"): Stop the garbage collector from running. Before calling restart, the collector will only run due to explicit calls.

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 result is as follows (note the change in memory usage):

20.9560546875
20.9853515625
0
19.4111328125