[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Tracking memory usage
- From: Matthew Wild <mwild1@...>
- Date: Thu, 13 Sep 2012 18:17:23 +0100
On 25 August 2012 08:11, Paul K <paulclinger@yahoo.com> wrote:
> 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?
Depending on what you mean by "proper number", possibly, yes. If you
want to only count live objects, and not temporary ones that have
already been freed, you need to force a GC run to find and clear the
freed ones away.
> 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:
Yes, if you want all memory ever allocated. Don't forget that the GC
may run of its own accord during the function execution. If you don't
want this to happen, collectgarbage("stop"). Be careful - running a
collection manaually automatically restarts it again.
> 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?
Take a look at http://code.matthewwild.co.uk/lua-getsize/ to get you
started. There has been some discussion about it on this list too, if
you search (the original code was not by me - I adopted and
improved/extended it because it was useful).
Regards,
Matthew