
LUAڴй¶

ο: http://www.cnblogs.com/yaukey/p/unity_lua_memory_leak_trace.html
GitHubַ: https://github.com/yaukeywang/LuaMemorySnapshotDump

ǰһʼͬһŻڴ棬Ż Lua ڴ棬Ϊÿս Lua ڴǳ󣬴 3M ھ10ҵս󣬻ᱩ 100Mڴй©
     Ȼʽù˼·ǵݹڴеݣЭ̣ûݣ鿴δͷźͱȫֱй©ͨҲοϣ
     http://shavingha.blog.163.com/blog/static/10378336200822134554488/
     http://blog.csdn.net/shimazhuge/article/details/43794347
     http://blog.codingnow.com/2012/12/lua_snapshot.html
     https://github.com/cloudwu/lua-snapshot/blob/master/snapshot.c
     http://stackoverflow.com/questions/11366693/lua-count-the-no-of-references-to-a-table
  Lua ٷֲᣨҪ
 
     иԵĲοֵҲЩȷͲҪĵطһĸڵ㲻Ǵ _G ʼǴ debug.getregistry ʼ©ܶݣҲ c дֱ lua ʵ֣ѽӡ txt 
     ҵʽԵݹķʽУ
 
 

    ÿһݽü weak table С
    ȫֱй©ϷӡһϷݣϷ˳ǰӡһڴݣȻѲĲٹҰôȻпɻ߲óֵȫֱһ㶼ڸڵ㣩ֱӶλ޸Ĵ룬ֱûȫֱй©λá
    Ϸ߼δͷţս߼й©ÿνӡһݣÿսɶصϻصսݶǱͷŵģȻԱдӡڴݣ첿ҰôȻݽŻ޸Ĵ߼ûͷŵĵطͷš
    ϵѭϷʽֱڴȶںԤڷΧڡ

     ͨϷʽĿе Lua ڴй©ʱϷLua ڴȶ 3.5M ң߷ʱ ᵽ 5M
 

2017-05-05 £
Դַhttps://github.com/yaukeywang/LuaMemorySnapshotDump
 

2017-07-01 
˵ Lua ڴй¶֮ǰĿ׵Ϊȫֱ⣬߶ڲȫֱڴй¶ЧԶ Lua 5.1Ӧе߼űװɳУLua 5.3  _ENV Чĸ⣬ҲӦýһװ߼űɳУ߼ԱôдȫֱҲ⣬Ψһڴй¶ĵطǴ C# ˵Ϸ͸ֽűԴ˴ĶԴȵپ˺ܶϷȻ޷õЧͷš
 

2017-07-21 
ܿ߷һЩѽ⣬ڸһЩѹͨзЩҪĽӿڷûϣ߹ʹòȷΪҳ׸ GitHub ҳϵʹ˵Ӣģټ˵¹ʹá
 require ű磺local mri = require(MemoryReferenceInfo)
Ȼĳطӡһڴգ

collectgarbage("collect")
mri.m_cMethods.DumpMemorySnapshot("./", "All", -1)

ļݣÿһһöϢеϢôУÿһб tab ֳ3Уֱǣ/ַôļʹ Excel 򿪣ԶΪ3УĶ

ļص㲿Ϣ "function: 0x7f85f8e0e3f0 registry.2[_G].Author.Ask[line:33@file:example.lua] 1" Ϣ˵ǣ "registry" ĳԱ "2"ҲǱ "_G"˱ "Author" "Author" һԱ "Ask"  "function: 0x7f85f8e0e3f0"λļ "example.lua" еĵ33Уһ1ΡܿٵĶλʲôﱻãһ˶ٴΡ

"DumpMemorySnapshot" ǡڵơ͡ڵ󡱣ĬֵΪ "registry"  "debug.getregistry()"ڴʹõʱҪ޸ʹĬֵɣǵӱĸڵ㿪ʼСΧ "_G" ֶ磺

-- Only dump memory snapshot searched from "_G".
collectgarbage("collect")
mri.m_cMethods.DumpMemorySnapshot("./", "All", -1, "_G", _G)

һʱٴӡһڴգԴӡݣҪĹǶԱȿշӵй¶㡣УṩһΪ DumpMemorySnapshotComparedFile ĽӿʵԱȹܣмǲҪԼļԱȹԱݿգùΪǸüģʱ䲻ͬҲͬ˳ҲͬͨļԱȹ޷Чġʹ÷

mri.m_cMethods.DumpMemorySnapshotComparedFile("./", "Compared", -1, 
"./LuaMemRefInfo-All-[1-Before].txt", 
"./LuaMemRefInfo-All-[2-After].txt")

һļǳڵڶݿﵫûвڵһݿݣݡ

͵ݣ dump ݹ󣬵鿴ĳضݣʹùһļѡļɵǰؼ֣ųؼ֣磺

-- ļаؼ Author ݡִСд
mri.m_cBases.OutputFilteredResult("./LuaMemRefInfo-All-[2-After].txt", "Author", true, true)

--ļвؼ Author ݡִСд
-- Filter all result exclude keywords: "Author".
mri.m_cBases.OutputFilteredResult("./LuaMemRefInfo-All-[2-After].txt", "Author", false, true)

⣬鿴ĳ󵽵ױЩطţʹýӿ "DumpMemorySnapshotSingleObject"磺
ƴ

--ö "_G.Author" ĵط 
collectgarbage("collect")
mri.m_cMethods.DumpMemorySnapshotSingleObject("./", "SingleObjRef-Object", -1, "Author", _G.Author)

-- ַ "yaukeywang" ĵط
collectgarbage("collect")
mri.m_cMethods.DumpMemorySnapshotSingleObject("./", "SingleObjRef-String", -1, "Author Name", "yaukeywang")

ƴ

ͨϼҪķʹãͿԿٵĲڴй©ʹֻҲʹãӡʱ·ָ sd Ŀ¼ʹ Unity  Luaʹã

collectgarbage("collect")
mri.m_cMethods.DumpMemorySnapshot(UnityEngine.Application.persistentPath, "All", -1)

һݿļ sd Ŀ¼¡

¼һѡһ "DumpMemorySnapshot" ָһ·ͶϢȻ󱣴ļÿζϵǰʱʱֲͬĿգҲҪƵú޸ļҲͬһطͬʱĿձϸǣʱѡĬϿͨ "mri.m_cConfig.m_bAllMemoryRefFileAddTime = false" رգõ÷ "require" Dump ֮ǰ Dump ĽӿҲǷ񸽼ʱļѡοԴ롣

ϵķṩһЩĽӿڿʹãϸʹο GitHub ϵ ReadMe ԴеĽӿڶ˵дĺϸˣ"Example.lua" Ҳʾ˳ýӿڵʹ÷

ߣַ͵·ͼ·Ͽһ "string"ͬʱҪע⣺ΪͬһʾַԷݽдԱȲExcel ͳƵȣַʾʱеĻسͻз'\r', '\n' ʾ滻 '\\n'ҪĶݵʱע⡣
Written by yaukey. ˵¾Ϊԭתעyaukeyͱ͵ַ(http://www.cnblogs.com/yaukey/)