lua-users home
lua-l archive

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


I'm sure there are people on the list who have answers to these
questions. Those who know don't tell? ;)

Paul.

---------- Forwarded message ----------
From: Paul K <paulclinger@yahoo.com>
Date: Sat, Aug 25, 2012 at 12:11 AM
Subject: Tracking memory usage
To: Lua mailing list <lua-l@lists.lua.org>

I've been reading about optimizing memory usage in Lua
(http://fmeus.wordpress.com/2009/01/19/optimizing-memory-usage-in-lua/
and http://bitsquid.blogspot.com/2011/08/fixing-memory-issues-in-lua.html)
and would like to be able to show memory statistics for my Lua
applications. I know about collectgarbage("count"), but I'm trying to
get more detailed information about memory allocations using available
Lua methods.

One thing that comes to mind is to track call/return events using
debug hook and take snapshots of allocated memory during those events.
Two questions related to that:
1. Do I need to call collectgarbage("collect") before calling "count"
to get a proper number?
2. This information is missing all the allocations that happen during
function execution. Or do I need to call "count" twice in the "return"
event to get the amount of allocated memory:

"call" event
  collectgrabage("collect")
  initial = collectgarbage("count")

"return" event
  allocated = collectgarbage("count")
  collectgarbage("collect") -- does this free the memory deallocated
in the blog?
  final = collectgarbage("count")

Does "final - allocated" tell me the amount of memory *temporarily*
allocated/released by the function/block?
Does "final - initial" tell me the amount of memory allocated by the
function/block in global/upvalue variables?

Also, is there any way to see how much memory is allocated by a
particular data structure (a table)? Even approximately/heuristically,
based on the number of array/hash elements it stores?

If none of this is possible using existing Lua methods, is there a
module available from Lua that provides that information? Thank you.

Paul.