lua-users home
lua-l archive

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


On 1 June 2010 15:33, zuric <zurrix@gmail.com> wrote:
> thanks very much.
> i did some test with your file, but i have a problem with that.
> when i getSize() of a table, the total size of the table will not include
> the refrence object.
> for example,
> test = {1,2}
> test1 = {1,2,3,4,5, link = test}
> the size of test1 will not include test
> any idea ?
>

Yes, that's intentional. Getting the "size" of a table including its
descendents isn't trivial sometimes, but if you have a simple tree
then it isn't hard. Just loop over all the elements and add up the
sizes, something like:

local function get_total_size(t)
    local size = debug.getsize(t);
    if type(t) == "table" then
        for k, v in pairs(t) do
            size = size + get_total_size(t);
        end
    end
    return size;
end

Of course this doesn't count metatables, function upvalues, and
possible other references. For a more comprehensive analysis of memory
usage I use http://code.matthewwild.co.uk/luatraverse/ which is based
on some code someone posted to this list once.

I'm eventually planning to release a project that ties these two
together, and produces a graphical view of what objects are using the
most memory. We use it for debugging memory leaks in Prosody, where it
has proved extremely useful.

Hope this helps,
Matthew