lua-users home
lua-l archive

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



Jorge,

* On 2009-04-01 Jorge <xxopxe@gmail.com> wrote  :

> I wonder how to profile memory usage. I have this application, a
> content-based router that moves notifications and subscriptions trough
> a p2p network. It's based around a socket server (LuaSocket based),
> and keeps lots of tables for peer lists, loop detection, subscription
> filters, and assorted stuff. The memory consumption fluctuates a lot
> with usage patterns, and there are a few configuration parameters that
> have a serious impact.  It's intended to be run on plain
> consumer-grade wireless routers (WRT54GL's and such), so RAM
> consumption is an issue.  I've googled around and only found stuff
> related to pre-5.1 lua,and wonder if there's an easy way to see what
> tables are consuming what.

As far as I know, there is no easy way to do this. A method I sometimes
use is to check the memory usage, dereference the table and all its
objects, and check memory usage again. This is of course destructive,
but for debugging purposes it gives a good idea of the memory used by a
table, if you keep in mind some internals about lua's bookkeeping.

Another method to give an rough estimation of the size would be to
recursively iterate the table, keeping count of the various objects you
encounter and adding up the memory of each object. You could do some
tests to determine the approximate size of various native lua objects,
and keeping in mind that strings are interned and only stored once and
that the numeric index of a table is always a power of two, you should
be able to get an idea of the size of a given table.