lua相互調用的內存釋放問題

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"))


輸出:

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似乎已經被釋放,中間的幾個數據因爲棧變量的影響意義不大。

如何才能更明確的知道這裏有沒有內存泄露?


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章