Brief Introduction to the garbage collection mechanism in Lua
This article mainly introduces the garbage collection mechanism in Lua. Automatic garbage collection is one of the important features of Lua. For more information, see
Lua uses automatic memory management for garbage collection based on some built-in Lua algorithms. The results of automatic memory management can be used as a developer:
There is no need to worry about allocating memory to objects.
You no longer need to set them to nil when you do not need to release them.
When Lua uses an object that runs from time to collect dead objects, it no longer accesses the garbage collector from the Lua program.
All objects, including tables, user data, functions, threads, strings, and so on, are automatically managed in memory. Lua uses the incremental tag and two numbers to control its garbage collection cycle, that is, the garbage collection pause and the garbage collector process get twice the result with half the effort to clear the collector. These values are always equal to 1 in percentage and 100.
Garbage collection paused
The garbage collection pause is used to control how long the Garbage Collector needs to wait before; it is called again by Lua's automatic memory management. If the value is less than 100, it means that Lua does not wait for the next cycle. A value similar to a higher value causes the Garbage Collector to be slow and less active in nature. 200 indicates that the total waiting time of the set is twice before the start of a new cycle. Therefore, depending on the nature and speed of the application, it is possible to change this value to obtain the best performance in the Lua application.
Step-by-Step Garbage Collector
This multiplier controls the relative speed at which garbage collection is distributed in Lua program's memory. A large step size will cause the Garbage Collector to be more aggressive and increase the step size for each incremental step of garbage collection. A value smaller than 100 may often prevent the garbage collector from completing its cycle and is generally not optimal. The default value is 200, which means the Garbage Collector runs twice the memory allocation speed.
Garbage Collector Function
As developers, we have indeed made Lua automatically manage memory. To solve this problem, you can use the following methods.
Collectgarbage ("collect"): a complete cycle for garbage collection.
Collectgarbage ("count"): returns the amount of memory of the program in kilobytes.
Collectgarbage ("restart"): If the Garbage Collector has stopped, it will be restarted.
Collectgarbage ("setpause"): Set to the second parameter divided by 100 to the value of the garbage collector pause variable. Its purpose is discussed above.
Collectgarbage ("setstepmul"): Set to the value of the variable set to the second parameter divided by the 100 to the spam step multiplier. Its purpose is discussed above.
Collectgarbage ("step"): a step for garbage collection. The second parameter is that the larger the value, the larger the step. True will be returned for the collected garbage, if the trigger step is the last step of a garbage collection cycle.
Collectgarbage ("stop"): stop the Garbage Collector if it runs.
A simple example of using the garbage collector is as follows.
The Code is as follows:
Mytable = {"apple", "orange", "banana "}
Print (collectgarbage ("count "))
Mytable = nil
Print (collectgarbage ("count "))
Print (collectgarbage ("collect "))
Print (collectgarbage ("count "))
When we run the above program, we will get the following output. Note that the results will be different, because the Lua automatic memory management function may also be different in the operating system.
The Code is as follows:
20.9560546875
20.9853515625
0
19.4111328125
As shown in the preceding procedure, the memory usage can be reduced once garbage collection is completed. However, it is not mandatory. Even if we do not call it, it will be automatically executed by the Lua interpreter after the specified time in the next stage.
Obviously, we can change the behavior if we need to use these features of the garbage collector. These features provide a little extra capability for developers to handle complex situations. Depending on the memory needs to execute the program type, may or may not use this function. But in the application memory usage, and in the program itself, to avoid the need to check the results after deployment.