Latest web development tutorials

Lua garbage collection

Lua uses automatic memory management. This means that you do not need to worry about the newly created object out how to allocate memory, they do not consider how they release the memory occupied after the object is no longer being used.

Lua is running agarbage collector to collect all dead objects(that is, objects can not be accessed in Lua) to perform automatic memory management. Lua is used in all the memory, such as: string, tables, user data, functions, threads, internal structure, etc., are subject to automatic management.

Lua implements an incremental mark - Scan collector. It uses two numbers to control the garbage collection cycle: garbage collector garbage collection rate and intermittent stepping magnification. Both figures use the percentage of units (for example: a value of 100 in the internal representation 1).

Garbage collection intermittently controls the rate needs to collect before opening a new cycle to wait long. Increasing this value will reduce the enthusiasm of the collector. When this value is less than 100, when the collector before opening the new cycle will not have to wait. Set this value to 200 will allow collectors to wait until twice the total memory usage reaches before the start of a new cycle.

The garbage collector stepper magnification controls the operating speed of the collector relative to memory allocation speed ratio. This will not only increase the value of the collector more aggressive, but also increase the length of each incremental step. Do not set this value to be less than 100, so if the collector to work slow so always so much to do a loop. The default value is 200, which means that the memory allocation to the collector of "double" speed work.

If you step magnification to a very large number (the number of bytes than you may be used in the program is also 10% larger), the collector acts like a stop-the-world collector. Then if you put the intermittent rate set to 200, the collector's behavior and past Lua versions of the same: each memory used by Lua doubled, to do a complete collection.


Garbage collector function

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

  • collectgarbage ( "collect"): do a full garbage collection cycle.The parameter opt which provides a different set of functions:

  • collectgarbage ( "count"): K to return the number of bytes total amount of memory used by Lua.This value has a fractional part, we only need to multiply 1024 you can get the exact number of bytes used by Lua (unless overrun).

  • collectgarbage ( "restart"): Restart the garbage collector runs automatically.

  • collectgarbage ( "setpause"): Set the arg collector intermittent rate (see sect; 2.5).Return to the previous value of the intermittent rate.

  • collectgarbage ( "setstepmul"): Returns the previous value of the step magnification.

  • collectgarbage ( "step"): one-step run the garbage collector.Step "size" by a arg control. 0 incoming collector stepper (indivisible) step. Incoming non-zero value, collector to collect the equivalent Lua allocation of these multiple (K bytes) of memory work. If the collector end of a cycle will return true.

  • collectgarbage ( "stop"): Stop the garbage collector runs.Before calling the restart, the collector only because of an explicit call to run.

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"))

The above program, the output results are as follows (note change in memory usage):

20.9560546875
20.9853515625
0
19.4111328125