Lua memory leak coping methods [GO]

Source: Internet
Author: User

Transfer from http://blog.csdn.net/xocoder/article/details/42685685

Since the project is currently in charge of a two-time development project, and left our code quality is not able to spit groove, so encountered a lot of large and small pits, fortunately, slowly trickling past. Recently encountered a memory leak problem, the leak occurred in Lua, the project code in the previous development team left a leak detection code, but also limited to this. Because of the large amount of code, it is logical to comb clear where the reference did not kill the memory leak is almost a needle in the haystack. Fortunately, the process is smooth, this article will talk about how to solve the problem of memory leaks in Lua.

As for how to find the memory leak, also simply say, if it is strange code, or although it is yours, but you do not bother to guess where the leak, then please refer to the Cloud Wind leak Check tool: http://blog.codingnow.com/2012/12/lua_snapshot.html

If the general logic of your code is familiar, you can use a weak reference table to check for leaks. often the leakage is a number of repeatedly created types , such as the Game of monsters (to kill a new one), the player (someone login to create a new player object), these things due to the program during the cycle of the creation and destruction, so once the destruction of the dirty situation, can easily lead to significant memory leaks. There is a relatively simple way to explore whether these objects are leaking, that is, weak reference tables . (If you don't know what a weak reference is, please click here).

In order to discover a memory leak, we can create a global weak reference table, make its key a weak reference , and then put it into this table every time it creates a potentially leaking object, so that it will usually be used as a key,value. Because of the nature of the weak reference, if the other reference disappears, the reference to the object in the weak reference table disappears (to nil), and the reference to the object in the weak reference table persists as long as there are any other references. Relying on this feature, when the program has run through the release of the object's logic, if there is still a reference to this object in the table, then this object must be leaked.

After talking about the method of discovering the leak, the next round is how to solve it. In fact, I would also like to try the cloud big snapshot, but this project is Luajit, inexplicably can not require, time is urgent, can not study, had to give up, another find his law. Behold a meal of Google down in addition to snapshot there is hardly a decent solution. Nasty under, had to study the principle, own hands, below please see Dry goods:

Since the memory leak must have a reference is not clear, then based on the characteristics of LUA, this reference must exist in the _g below a table or function of the upvalue (do not understand this classmate please want to understand and look down), since the first step of the method can locate the leak, And you can get a reference to the leaking object, then things are much better. It is simply to get a reference to traverse _g, find this reference, and the hierarchy column out, convenient to know where this thing is exactly where, want to solve is good to do more. The implementation method is not much to say, look at the most direct code. In the following code, call Findobjectinglobal (a reference to the leaking object) to find the attribution of all non-local variables.

Local Findedobjmap =nil function _g.findobject (obj, finddest)ifFinddest = =Nil Thenreturn falseEndifFindedobjmap[finddest] ~=Nil Thenreturn falseEnd Findedobjmap[finddest]=trueLocal Desttype=type (finddest)ifDesttype = ="Table" ThenifFinddest = =_g.cmemorydebug Thenreturn falseEnd forKey, ValueinchPairs (finddest) Do              ifKey = = Obj or value = =obj then _info ("finded Object")                  return trueEndifFindobject (obj, key) = =trueThen _info ("Table Key")                  return trueEndifFindobject (obj, value) = =trueThen _info ("key:[".. ToString (Key):"]")                  return trueend End ElseIf desttype=="function"Then local Uvindex=1           while true  Dolocal name, value=Debug.getupvalue (finddest, Uvindex)ifName = =Nil Then BreakEndifFindobject (obj, value) = =trueThen _info ("Upvalue name:[".. ToString (name):"]")                  return trueEnd Uvindex= Uvindex +1End Endreturn falseEnd Function _g.findobjectinglobal (obj) findedobjmap={} setmetatable (Findedobjmap, {__mode="k"}) _g.findobject (obj, _g) End

Lua memory leak coping methods [GO]

Related Article

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.