Lua Garbage Collection
# Lua Garbage Collection
Lua uses automatic memory management. This means you don't have to worry about how to allocate memory for newly created objects, nor how to release memory occupied by objects that are no longer in use.
Lua runs a **garbage collector** to collect all **dead objects** (i.e., objects that are no longer accessible in Lua) to accomplish automatic memory management. All memory used in Lua, such as strings, tables, user data, functions, threads, internal structures, etc., is subject to automatic management.
Lua implements an incremental mark-and-sweep collector. It uses two numbers to control the garbage collection cycle: the garbage collector pause and the garbage collector step multiplier. Both numbers are expressed as percentages (for example, a value of 100 represents 1 internally).
The garbage collector pause controls how long the collector needs to wait before starting a new cycle. Increasing this value reduces the collector's aggressiveness. When this value is less than 100, the collector will not wait before starting a new cycle. Setting this value to 200 will cause the collector to wait until total memory usage reaches twice the previous amount before starting a new cycle.
The garbage collector step multiplier controls how fast 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 below 100, as that would make the collector work too slowly to ever finish a cycle. The default value is 200, which means the collector works at "twice" the speed of memory allocation.
If you set the step multiplier to a very large number (10% more than your program might ever use in bytes), the collector behaves like a stop-the-world collector. Then if you set the pause to 200, the collector behaves like previous Lua versions: it performs a complete collection every time Lua's memory usage doubles.
* * *
## Garbage Collector Functions
Lua provides the following function **collectgarbage ([opt [, arg]])** to control automatic memory management:
* **collectgarbage("collect"):** Performs a complete garbage collection cycle. Through the opt parameter, it provides a set of different functions:
* **collectgarbage("count"):** Returns the total memory used by Lua in kilobytes. This value has a fractional part, so multiplying by 1024 gives the exact number of bytes used by Lua (unless it overflows).
* **collectgarbage("restart"):** Restarts the automatic operation of the garbage collector.
* **collectgarbage("setpause"):** Sets arg as the collector's pause. Returns the previous value of the pause.
* **collectgarbage("setstepmul"):** Returns the previous value of the step multiplier.
* **collectgarbage("step"):** Runs the garbage collector in single-step mode. The step "size" is controlled by arg. When passing 0, the collector performs one (indivisible) step. When passing a non-zero value, the collector performs work equivalent to Lua allocating that much (in kilobytes) of memory. Returns true if the collector finishes a cycle.
* **collectgarbage("stop"):** Stops 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:
## Example
mytable ={"apple","orange","banana"}
print(collectgarbage("count"))
mytable =nil
print(collectgarbage("count"))
print(collectgarbage("collect"))
print(collectgarbage("count"))
Executing the above program produces the following output (note the changes in memory usage):
20.956054687520.9853515625019.4111328125
YouTip