2

I'm using NLua to compile lua code from C#. The problem is that LuaTables created in C# can't be fully disposed by Lua garbage collector.

Here is the sample code:

        public static List<LuaTable> TempTables = new List<LuaTable>();

    static void Main(string[] args)
    {
        using (Lua lua = new Lua())
        {
            double mem = (double)lua.DoString("return collectgarbage(\"count\")")[0];
            for (int i = 0; i < 100000; i++)
            {
                LuaTable lt = NewTab(lua);
            }


            mem = (double)lua.DoString("return collectgarbage(\"count\")")[0];
            foreach (LuaTable lt in TempTables)
            {
                lt.Dispose();
            }
            TempTables.Clear();

            lua.DoString("collectgarbage()");
            mem = (double)lua.DoString("return collectgarbage(\"count\")")[0];
        }
    }
    public static LuaTable NewTab(Lua l)
    {            
        LuaTable lt = (LuaTable)l.DoString("return {}")[0];
        TempTables.Add(lt);
        return lt;
    }

This code simply creates 100 000 tables, then disposes it in C# and calls garbage collection in lua. Memory used can be inspected in variable "mem". The result is that "mem" will increase from 22.04Kb to approx. 1000Kb. It means that disposing this way will lead to memory leakages.

However, if we dispose LuaTable immedeately:

            for (int i = 0; i < 100000; i++)
            {
                LuaTable lt = NewTab(lua);
                lt.Dispose();
            }

It works well. The "mem" stays the same +-1Kb.

Why is it so? How should I dispose Lua objects to make it fully accessible to lua gc?

Vyzhlakov
  • 21
  • 1
  • Try calling `collectgarbage()` twice; I don't think Lua collects and finalizes in one full cycle. – Colonel Thirty Two Aug 10 '14 at 01:54
  • #Colonel Thirty Two I've tried 2-3-4 times collectgarbage() but nothing's changed. The most strange issue for me is: why the garbage collected rightly if we Dispose C# object immedeately and why it fails when we Dispose the same object after sometime. As I understand there's no difference wheter I dispose the temproray object or add this object to List and then dispose it in this List. – Vyzhlakov Aug 11 '14 at 11:44
  • This is a flaw in the LuaInterface/NLua design. You need to manually keep track of your LuaObjects and dispose then. The Lua gc can't know when you are no longer using the table (or function) so I think the best approach is to create a list of disposable tables, or use the using pattern. – Vinicius Jarina Aug 12 '14 at 02:33
  • @ViniciusJarina This is exactly what I did. As you see each table that I created I store in list "TempTables" and then dispose it. However by unknown reason the memory leaks. The only way to avoid memory leaks is to dispose the LuaTable immedeately but this way is not working for real application. – Vyzhlakov Aug 12 '14 at 17:46
  • @user3925631 Try to repro this memory leak on a small program and send a PR to NLua/NLua project. I can try to see what is wrong. Thank you. – Vinicius Jarina Aug 12 '14 at 19:27
  • @ViniciusJarina I've sent an e-mail to you with the code. Have you received it? – Vyzhlakov Aug 20 '14 at 11:47

0 Answers0