[Lua] Lua Memory leakage detection principle

Source: Internet
Author: User

[Lua] Lua Memory leakage detection principle
Lua Memory leakage

First, the memory leakage in lua is essentially different from the memory leakage in c/c ++.

Lua has a garbage collection mechanism (GC), So theoretically there will be no memory leakage.When it performsDuring GC, all objects will be scanned from the root. If there is a reference to this object somewhere, this object will not be collect in memory, and this object will not be GC.Therefore, the memory leakage in lua refers to the following:It is not used, but there are still referenced objects.

-- The object declared as local in the function will not add locallocal function test () testTable = {}-- this testTabel will be stored in global table _ G, during GC, a table is always leaked because this object still has references. Local mt = {} -- mt is decorated with local. After the function is called, the reference will no longer exist, and GC will be recycled. Setretriable (testTable, mt) end
Detection Principle

There are five objects that support the garbage collection mechanism in lua:String, table, function, full userdata, thread.And their references are stored directly or indirectly:Lua_state object, _ G global table, Registry, global_state-> mt.

In the script:

The running lua script itself is lua_state. _ G is the _ G global table. The Registry table can be obtained using debug. getregistry. Global_mt can be obtained using debug. getretriable.

Therefore, we can implement the memory leak detection module at the script level.

Notes for searching:

If the value of _ mode in a retriable table is k, v, or kv, special processing is required (as described in the supplement). function searches for enviroment, it is also a table. Search upvalues for any type. Because userdata cannot be modified at the script level, the thread object of searching for his retriable is a coroutine object. Generally, no multiple coroutine is created in the script, so it is not searched in the script. If necessary, obtain the thread function, and then follow step 1.

Search flow chart (_ G table)

Before detecting leaks, search for all objects, save the initial memory status, perform several GC operations after the program is executed, and then perform a search, comparing the two results, the extra results may be Memory leakage. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + values/LXEbWV0YXRhYmxl1tC1xF9fbW9kZbG7yejWw86qobBr "," v "or" kv ", indicates that the key, value, or key value stored in it is a weak reference state.
If all references to an object are weak references, the object will be recycled by GC, so the corresponding entry to this object in the weak table will be absent.

So we can use another method: to drop all the resource objects created by the user into the weak table, force GC after running the program, and then view the weak table, if the table still stores the object, it means that this object has external references (we call it a strong reference for a relatively weak reference), and the resource is not GC, so we can say that this object is likely to cause memory leakage.

Lua garbage collection Algorithm

The GC algorithm of Lua uses the so-called "Mark And Sweep" algorithm. This algorithm divides GC into two phases,One is to mark (Mark), Which marks all objects referenced in the system one by one;While cleaning (Sweep). data that is not marked in the mark phase will be deleted.

In Lua, use several colors to differentiate nodes:

White: white indicates a node that has not been marked. gray indicates a node that has been marked, but the node associated with it has not been marked. Black: the current node and associated nodes have been scanned and marked. Usually there will be associated data, including Table, upvalue, and other data types. Garbage Collector Function

The collectgarbage function provides multiple functions:Stop garbage collection,Restart garbage collection,Execute a recycling cycle forcibly,Enforce one-step garbage collection,ObtainLuaMemory occupied, AndTwo parameters that affect the garbage collection frequency and stride. Collectgarbage (opt, [, arg])

"Stop"

Stop the Garbage Collector if it is running.

"Restart"

If the Garbage Collector has stopped, it will be restarted.

"Collect"

Execute a full garbage collection cycle. This operation is performed by default.

"Count"

Returns the memory used in the current Lua (in KB)

"Step"

One garbage collection step is executed in one step. The step Size is specified by the arg parameter (large values must be completed in multiple steps). To accurately specify the step Size, multiple experiments are required to achieve the optimal effect. True is returned if the step is collected once.

"Setpause"

Set the value of arg/100 as the duration of the tentative collection, and return the value before the setting. The default value is 200.

Controls how long the collector will wait before a new collection cycle starts. As the number increases, the collector becomes less active. A value smaller than 1 means that the collector does not wait at the beginning of the new cycle. When the value is 2, it means that the new cycle will be enabled when the total memory usage reaches twice the original number.

"Setstepmul"

Set the value of arg/100 as the step increase (that is, the new step = the old step * arg/100), and return the value before setting. The default value is 200.

It controls the speed of the collector, which is relative to the memory allocation speed. A larger number will make the collector more active and increase the size of each step. A value smaller than 1 slows down the operation of the collector, which may cause the collector to never end the current cycle. The default value is 200%, which means the collector will run at twice the speed of the memory distributor.

Function test1 () collectgarbage ("collect") -- to have a clean environment, collect the garbage that can be collected. collectgarbage () -- to ensure that the memory is relatively clean, and memory stability. Execute print ("now, Lua memory is:", collectgarbage ("count ")) --> 205.7158203125 KB local colen ={} -- now the local variable for I = 5000 do table. insert (colen, {}) end print ("now, Lua memory:", collectgarbage ("count") --> 860.4111328125 KB -- create 5000 tables, memory added 655 KBendfunction collect1 () print ("now, Lua memory:", collectgarbage ("count") --> 608.060546875 KB collectgarbage () print ("now, Lua memory is:", collectgarbage ("count") --> 204.8408203125 KB -- the last difference from the beginning is only 1 KBend
Function test2 () collectgarbage ("collect") -- to have a clean environment, collect the garbage that can be collected. collectgarbage () -- to ensure that the memory is relatively clean, and memory stability. Execute print ("now, Lua memory is:", collectgarbage ("count ")) --> 205.7158203125 KB colen ={} -- now all variables for I = 5000 do table. insert (colen, {}) end print ("now, Lua memory:", collectgarbage ("count") --> 619.826171875 KB -- create 5000 tables, the memory is increased by 414 KB. These memory increases will never be recycled because they have been stored in global functions! Endfunction collect2 () print ("now, Lua memory is:", collectgarbage ("count") --> 596.7822265625 KB collectgarbage () print ("now, lua memory: ", collectgarbage (" count ") --> 489.189453125 KB -- the last increase in memory is 284KB (489-205) end

The garbage collector has two parameters to control its pace:

The first parameter, called the pause time, controls how long the recycler will wait after a collection is completed and before the next collection starts;

The second parameter, called the step coefficient, controls the amount of content recycled by each step of the recycler. Roughly speaking, the smaller the pause time and the larger the stepping coefficient, the faster the garbage collection. The impact of these parameters on the overall performance of the program is unpredictable. A faster garbage collector will obviously waste more CPU cycles, but it will reduce the total memory consumption of the program, this may reduce paging. Only careful testing can give you the best parameter value.

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.