lua-users home
lua-l archive

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


> Is there an easy way to just get basic diagnostic information about a lua
> state?  How much memory it is using, etc?  I have a system in which an
> environment is created and then periodically called back.  Over time, it
> tends to get slower and I think something may be leaking.  I need a little
> more diagnostic data to help track it down.

Well you can use collectgarbage("count") from within Lua and lua_gc(L,
LUA_GCCOUNT, 0) from C to get the memory used by Lua in kilobytes but
regarding to what's causing the leak you can't get that information
from Lua itself.  You'll have to somehow keep track of allocated
objects yourself (at least the ones that should matter in terms of
used memory) and see what hasn't been collected but should have been.
Then you'll also have to figure out why it hasn't been collected.
Perhaps others know of a simpler or more automated approach.

In the meantime I'm wondering aloud whether the Lua API itself might
not expose this information.  Obviously the GC has to know exactly why
a value can or cannot be collected as well as what values exist in the
state.  Would it be too much of a nuisance to the C API to expose a
list of allocated values as well as what references exist for each
one?  Perhaps it would be although one could reason that the API would
thus not expose the mechanism of garbage collection itself (thus
losing freedom over what the inner workings of this mechanism are) but
only the semantics of garbage collection which are anyway well defined
by the language.

In any case this can be done outside of the language as well but you
have to be sure that you've taken every possible kind of reference
(upvalues, table keys/values, etc.) into account.  I remember there
was some sort of code snippet to do this job but I can't remember a
URL right now.  Perhaps someone else can.

Dimitris