views:

101

answers:

3

I'd like to find out how much memory a lua table is using - without iterating through the table contents and counting up the usage. Is there a lua 5.1 function or 3rd party library that could help with this.

Thanks!

+2  A: 

There is no function for this task. Why do you want to do this? What are you trying to achieve?

lhf
I'm working with a lua app that is consuming a lot of memory - and ultimately eating up the available RAM. Frankly, it's a design flaw and I can see in the problem areas in the source code. A rewrite/rethink is needed but I can't get started on that for a month. In the interim I'm looking for a quick fix to get a customer up and running again. I figured a more detailed breakdown of the memory usage would help me to decide which part(s) to change/workaround now.
Have you tried tweaking garbage collection or using the emergency garbage collection patch? http://lua-users.org/wiki/EmergencyGarbageCollectorIt's quite stable, and also rather useful for even more embedded situations (64k SRAM on a microcontroller :-)Lua 5.2 pre-release versions also has an EGC, which is not the above patch, but should provide similar functionality.Are you actually using up all available memory with objects that are still in use?As you might find noted on the Lua list links in one of the other replies, Lua allows you to define whatever you want as a memory allocator.
James Snyder
+1  A: 

You can monitor the memory usage of LUA by calling collectgarbage("count") or gcinfo() in the appropriate locations throughout the code (e.g. before and after insert operations). There's no trivial way to get the size of a table.

fbcocq
Please never write Lua in all-caps. It is not an acronym. http://www.lua.org/about.html#name
Alexander Gladysh
I'll give this a go - it may give me enough info to make decisions. If not, I'll be looking into Alexander's and James' suggestions. Thanks!
A: 

Wouldn't something like this or this help?

Alexander Gladysh