lua相互呼叫的記憶體釋放問題
阿新 • • 發佈:2019-01-30
function test1( ... )
local t = {
name = "t"
}
function t:removeMe( arg )
--collectgarbage()
--print(collectgarbage("count") .. 1)
arg:remove()
--collectgarbage()
--print(collectgarbage("count") .. 3)
end
return t
end
function test( ... )
local t = {
name = "ta",
ut = test1()
}
function t:remove( ... )
self.ut = nil
--collectgarbage()
--print(collectgarbage("count") .. 2)
end
return t
end
function mainTest( ... )
function mainTest( ... )
local bt = test()
local function funct( ... )
local at = test1()
bt.ut = at
end
collectgarbage()
print(collectgarbage("count"))
funct()
print(collectgarbage("count"))
bt.ut:removeMe(bt)
collectgarbage()
print(collectgarbage("count"))
end
end
mainTest()
collectgarbage()
print(collectgarbage("count"))
local t = {
name = "t"
}
function t:removeMe( arg )
--collectgarbage()
--print(collectgarbage("count") .. 1)
arg:remove()
--collectgarbage()
--print(collectgarbage("count") .. 3)
end
return t
end
function test( ... )
local t = {
name = "ta",
ut = test1()
}
function t:remove( ... )
self.ut = nil
--collectgarbage()
--print(collectgarbage("count") .. 2)
end
return t
end
function mainTest( ... )
function mainTest( ... )
local bt = test()
local function funct( ... )
local at = test1()
bt.ut = at
end
collectgarbage()
print(collectgarbage("count"))
funct()
print(collectgarbage("count"))
bt.ut:removeMe(bt)
collectgarbage()
print(collectgarbage("count"))
end
end
mainTest()
collectgarbage()
print(collectgarbage("count"))
輸出:
26.2138671875
26.3115234375
26.1162109375
25.9248046875
[Finished in 0.3s]
註釋func函式結果:
26.2041015625
26.2041015625
26.1064453125
25.9150390625
問題:
在mainTest生命週期內,期望結果是在第4次collectgarbage時釋放記憶體
加入at,remove前相差約0.1,remove之後相差約0.2,不加入at,remove函式前後相差約0.1,單純估測at輸出值也約0.1(未列出),雖然有誤差,但是可以看得出at似乎已經被釋放,中間的幾個資料因為棧變數的影響意義不大。
如何才能更明確的知道這裡有沒有記憶體洩露?