Tuesday, October 04, 2016

A Lua Memory Viewer

If you're new to Lua you might not know about "_G". "_G" is the global table: the table containing all objects currently in global memory. All the Lua libraries are in there, any function or variable you define is there.

When writing a large program, it can be useful to have a look inside this "_G" table to find memory leaks or monitor variable values. To this end, I've written a simple visual exploration tool that let's you navigate your global space. I wrote the program using Dinodeck as it uses some graphical functions.

Here's how it works:

You set up the Watch and call it's Render and Update functions. The Render function draws it on the screen. The Update function lets you navigate through the table using the keyboard (left, right, space, backspace).

The watch takes some filters that let you filter out common things you'll find in the "_G" table. For instance Lua functions and keywords such as table, ipairs, os etc.

Here's an example of it in action.

In this example there's a table in the main.lua file that looks like this:

a = {b = {c = {d = { text = "Hello"}}}, v = Vector.Create(0.5, 1, 2, 3)}
The values will update in realtime as they change in the program.

Source Code

A copy of the file is available here as a gist.
At some point I'll add a proper project to the dinodeck repo.
It uses these Dinodeck libraries:
  • Renderer
  • Vector
  • Keyboard

Further Work


This project could have more polish and features. Here are some ideas:
  • Show a history value of the selected value (like how a number changes over time)
  • Show the colour of a vector when selected
  • Add a simple console so the list can be filtered by typing in what you want
  • Edit values
  • Sort the table entries alphabetically
  • Trim key/value labels so they always fit in the boxes
  • Nicer transitions for moving through tables and highlighting key/values
  • Make the code a little cleaner. `keyData` gets passed around too much at the moment
  • Support display of other Dinodeck types like sprite, matrix etc