Tracing a memeory leak in Lua

classic Classic list List threaded Threaded
2 messages Options
Tim Michals Tim Michals
Reply | Threaded
Open this post in threaded view
|

Tracing a memeory leak in Lua

This is more of a general Lua question: global Lua memory keeps rising, for example printing out collectgabarge("count")
and the memory keeps rising, also, I do collectgabarge("collect"), does not help.  So, guess there has to be some table growing in memory.
Is there a way to dump all of the tables and table sizes? or what is the best method to track this down?

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
jbsnyder jbsnyder
Reply | Threaded
Open this post in threaded view
|

Re: Tracing a memeory leak in Lua

Are you writing just pure Lua code or using the C API? It's much easier to accidentally have memory leaks from the C side (letting the stack grow, allocating objects and anchoring them in the registry, etc..).  If things are growing within the memory reported by collectgarbage they should be objects that are being tracked by the VM, but are still referenced by something.

Towards the end of locating what's building up, you can walk through the global table (_G) and enumerate everything that's connected to that part of the state.  You should be able to find some table dumping code, maybe something like this: https://github.com/kikito/inspect.lua

Or one of the examples here:
http://lua-users.org/wiki/TableSerialization

For the registry side of things, you can use the debug library to get the contents of the registry:
http://www.lua.org/manual/5.1/manual.html#5.9

Additionally, is your code only able to run on an MCU or can you use it on a desktop as well?  Some of the Lua debugging tools are probably easier to use from a desktop environment without modification:
http://lua-users.org/wiki/DebuggingLuaCode

It would be nice to have some simple way to dump and visualize the interconnection of objects in memory from a Lua state, but I'm not sure if there's an existing tool that will just do this for you.  I know there are such tools for Python like objgraph  (http://mg.pov.lt/objgraph/), but I haven't seen something like this for Lua yet.

--
James Snyder
Biomedical Engineering
Northwestern University
http://fanplastic.org/key.txt
ph: (847) 448-0386



On Wednesday, April 25, 2012 at 8:23 AM, Tim michals wrote:

> This is more of a general Lua question: global Lua memory keeps rising, for example printing out collectgabarge("count")
> and the memory keeps rising, also, I do collectgabarge("collect"), does not help. So, guess there has to be some table growing in memory.
> Is there a way to dump all of the tables and table sizes? or what is the best method to track this down?
> _______________________________________________
> eLua-dev mailing list
> [hidden email] (mailto:[hidden email])
> https://lists.berlios.de/mailman/listinfo/elua-dev



_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev