The garbage collection mechanism of LUA

Source: Internet
Author: User

The LUA garbage collection mechanism uses a method called flags and Sweeps (mark-and-sweep).


A recycling mechanism
The basic mark-and-sweep algorithm is one of the oldest garbage collection algorithms for solving circular reference situations.
As the name implies, this is a two-phases algorithm, which can be described in simple words:
(1) Mark phase (sign stage)
1> each GC-able object has a flag bit, initially 0 (unmarked).
The 2> defines the first accessible collection of objects in the program as a collection of root objects (root set).
3> recursively iterates through the reference relationships of all objects in the root collection, and if an object has a flag bit of unmarked,
The flag is 1 (marked).


(2) Sweep phase (Sweep stage)
1> traversal of all existing objects: releasing the flag or unmarked object;
The object marked as marked is also re-marked as unmarked, preparing for the next GC.


Two recovery functions
1 related functions
The CollectGarbage function provides several functions:
Stop garbage collection;
Restart garbage collection;
Enforcement of a recycling cycle;
Enforcement of one-step garbage collection;
Get the memory that Lua occupies;
and two parameters that affect the frequency and stride of garbage collection.


2 Function Prototypes:
CollectGarbage (Opt,[,arg])


3 Parameter Description:
"Stop" stops the garbage collector if it is running.


"Restart" If the garbage collector has stopped, it will be restarted.


"Collect" performs a full garbage collection cycle, which is done by default.


"Count" returns the amount of memory used in the current LUA (in kilobytes)


Step to perform a single garbage collection.
The step "size" is specified by the parameter arg (large values require multiple steps to complete),
If you want to accurately specify the step size, you need to experiment multiple times to achieve optimal results.
Returns true if the step completes a collection loop at a time.


"Setpause" sets the value of arg/100 as the amount of time provisionally collected and returns the value before it is set. The default is 200.
Controls how long the collector waits before starting a new collection cycle. As the number increases, the collector's work is less active. A value less than 1 means that the collector no longer waits at the beginning of a new cycle. A value of 2 means that a new cycle is opened when the total amount of memory used is twice times the original.


"Setstepmul" sets the value of arg/100.
As a step increment (that is, the new step = Old step *arg/100), and returns the value before the setting. The default is 200.
Controls the working speed of the collector, which is a speed relative to the memory allocation.
Larger numbers will cause the collector to work more aggressively at the same time, and also to increase the size of each step collected.
A value less than 1 causes the collector to work very slowly, which may cause the collector to never end the current cycle.
The default value is 200%, which means that the collector will run at twice times the memory allocator.


Reference:
http://www.douban.com/group/topic/16897102/

Http://www.2cto.com/kf/201502/377646.html

Http://www.lua.org/manual/5.1/manual.html#pdf-collectgarbage

The garbage collection mechanism of LUA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.