lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Shmuel Zeigerman wrote:
[...]
This is how my application was doing originally. But unfortunately, Lua tables used as dictionaries won't always reclaim memory when their entries are deleted. After several millions creations and deletions of userdata, this (empty!) table was holding > 100 MBytes of RAM that could not be reclaimed with garbage collector.

That sounds very much like a reference leak in your code rather than anything in the Lua core --- tables are used *so* much that any GC bug that keeps references to deleted items would have shown up elsewhere by now. The GC is pretty solid.

There are two very common reasons for reference leaks:

(a) keeping a reference in Lua code somewhere, possibly by accident:

    local mytable = {foo = BigObject()}

    local function dosomething(o)
            t = {} -- oops! should be local
	    t[#t+1] = mytable.foo
            dosomethingelsewith(t)
    end

    dosomething(mytable.foo)
    mytable.foo = null
    -- we now have a reference to BigObject() lurking in t, which won't
    -- go away until we reset t (possibly by calling dosomething()
    -- again).

(b) storing a reference in C and then forgetting to dereference it again once you're done:

    int leakme(lua_State* L)
    {
       // reference the top item on the stack
       int r = lua_ref(L, LUA_REGISTRY);
       // forget to dereference it
       // lua_unref(L, LUA_REGISTRY, r);
       return 0;
    }

    leakme(mytable.foo) -- BigObject() will now never be GC'd

Are you doing anything like this?

--
David Given
dg@cowlark.com