Lua garbage collection
Lua
automatic memory management is adopted. This means that you don’t have to worry about how to allocate the memory needed by newly created objects, or how to free up the memory occupied by objects after they are no longer in use.
Lua
garbage collector is run to collect all dead objects that is, in the Lua
objects that can no longer be accessed in to complete automaticmemory management. Lua
all memory used in, such as strings, tables, user data, functions, threads, internal structures, etc., are subject to automatic management.
Lua
incremental mark-scan collector is implemented. It uses these twonumbers to control the garbage collection cycle: the garbage collector intermittent rate and the garbage collector step rate. Both numbers use percentages (for example, a value of 100 represents 1 internally).
The intermittent rate of the garbage collector controls how long the collector has to wait before starting a new cycle. Increasing this value reduces the enthusiasm of the collector. When this value is less than 100, the collector will not wait before starting a new loop. Setting this value to 200 causes the collector to wait until the total memory usage reaches twice as much as before before starting a new loop.
The garbage collector step magnification controls the ratio of the collector’s operating speed to the memory allocation speed. Increasing this value not only makes the collector more active, but also increases the length of each incremental step. Do not set this value to less than 100, so the collector will work so slowly that it will never finish a cycle. The default value is 200, which means that the collector works at “twice” the speed of memory allocation.
If you set the step magnification to a very large number (10% more than the number of bytes your program might use), the collector behaves like a stop-the-world
collector. Then if you set the intermittent rate to 200, the behavior of the collector will be the same as in the past. Lua
the version is the same: every time Lua
when you double the amount of memory used, do a complete collection.
Garbage collector function
The following Lua
functions are provided collectgarbage ([opt [,arg]])
used to control automatic memory management:
collectgarbage("collect")
: Do a complete garbage collection cycle pass parameteropt
, it provides a different set of functions:collectgarbage("count")
: returned in K bytesLua
total amount ofmemory used. This value has a decimal part, so you only need to multiply itby 1024.Lua
exact number of bytes used (unless overflowed).collectgarbage("restart")
: Restart the automatic operation of the garbage collector.collectgarbage("setpause")
: setarg
to the intermittent rateof the collector. Returns the previous value of the intermittent rate.collectgarbage("setstepmul")
: Returns the previous value of the step magnification.collectgarbage("step")
: Run the garbage collector step by step The stepsize is determined byarg
control. When 0 is passed in, the collector takes a (indivisible) step. Pass in a non-zero value, and the collector collects the equivalent ofLua
work of allocating this much (K-byte) memory. If the collector ends a loop, it returnstrue
.collectgarbage("stop")
: Stop the garbage collector from running The collector runs only because of explicit calls before the call is restarted.
The following demonstrates a simple example of 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 is as follows (note the change in memory usage):
20.9560546875
20.9853515625
0
19.4111328125