我发现在一个C#/Lua LuaInterface项目中存在着内存泄漏问题。我已经在C#中编写了一个简单的测试函数,每隔0.5秒从Lua中循环调用。我可以看到每次循环时Lua的内存使用量都在增加。我的最新C#代码如下:
public LuaTable testMemTable()
{
LuaTable tabx = m_lua.GetTable("tabx");
if (tabx != null)
{
tabx.Dispose();
tabx = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
m_lua.NewTable("tabx");
tabx = m_lua.GetTable("tabx");
for (int i = 0; i < 20000; i++)
tabx[i] = i * 10;
return tabx;
}
尽管执行了tabx.Dispose()和tabx=null,然后强制进行GC,我仍然看到内存没有被释放。由于没有LuaInterface函数可以释放先前分配的表格,所以我不知道还能做什么来释放内存?
Lua端代码非常简单。
while true do
myAPILibs.testMemTable()
if tabx ~= nil then
print(string.format("Size = %d", #tabx))
else
print(string.format("Size = nil"))
end
print(tabx, tabx[111])
myAPILibs.sleep(500)
print("Before ", collectgarbage("count") * 1024)
collectgarbage("collect")
print("After ", collectgarbage("count") * 1024)
end
非常感谢您能帮助解决我的内存泄漏问题。
再次感谢。
Geoff