lua-users home
lua-l archive

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


Hello,

  I've been messing around with LUA since a few weeks now, and I'm
wondering something. How exactly does the __gc metamethod works ? It just
seems I'm not using it the right way. Look, if I use this piece of code:

function showstats()
    local m, t
    m, t = gcinfo()
    print("Memory used: " .. m .. ", threshold = " .. t)
end

function mygc(t)
    print("Inside of GC, over object " .. t.n)
end

function createobj(n)
    local t = {n = n}, mt
    mt = {__gc = mygc}
    setmetatable(t, mt)
    return t
end

function testing()
    local obj, i

    for i = 1, 30, 1 do
        print("Creating object number " .. i)
        obj = createobj(i)
        showstats()
    end
end

showstats()
print "Starting test"
testing()
print "Launching garbage collecting"
collectgarbage()
showstats()
print "Exitting"

and launch it using lua 5.0, I get this (sorry for the long paste, but
here we really see the garbage collector works...)

Memory used: 17, threshold = 24
Starting test
Creating object number 1
Memory used: 17, threshold = 24
Creating object number 2
Memory used: 18, threshold = 24
Creating object number 3
Memory used: 18, threshold = 24
Creating object number 4
Memory used: 18, threshold = 24
Creating object number 5
Memory used: 18, threshold = 24
Creating object number 6
Memory used: 19, threshold = 24
Creating object number 7
Memory used: 19, threshold = 24
Creating object number 8
Memory used: 19, threshold = 24
Creating object number 9
Memory used: 19, threshold = 24
Creating object number 10
Memory used: 20, threshold = 24
Creating object number 11
Memory used: 20, threshold = 24
Creating object number 12
Memory used: 20, threshold = 24
Creating object number 13
Memory used: 20, threshold = 24
Creating object number 14
Memory used: 21, threshold = 24
Creating object number 15
Memory used: 21, threshold = 24
Creating object number 16
Memory used: 21, threshold = 24
Creating object number 17
Memory used: 21, threshold = 24
Creating object number 18
Memory used: 22, threshold = 24
Creating object number 19
Memory used: 22, threshold = 24
Creating object number 20
Memory used: 22, threshold = 24
Creating object number 21
Memory used: 22, threshold = 24
Creating object number 22
Memory used: 23, threshold = 24
Creating object number 23
Memory used: 23, threshold = 24
Creating object number 24
Memory used: 23, threshold = 24
Creating object number 25
Memory used: 23, threshold = 24
Creating object number 26
Memory used: 23, threshold = 24
Creating object number 27
Memory used: 16, threshold = 33
Creating object number 28
Memory used: 16, threshold = 33
Creating object number 29
Memory used: 17, threshold = 33
Creating object number 30
Memory used: 17, threshold = 33
Launching garbage collecting
Memory used: 16, threshold = 32
Exitting

  See? At the creation of the object 27, the garbage collector is
launched. But none of my __gc metamethod was called. Same thing at the
exit, where a bit of memory was freed. So, what am I doing wrong ?

  I would really be grateful for any hint here. Thanks,

  -- Nicolas Noble