C programmers think memory management is too important to be left to the computer. Lisp programmers think memory management is too important to be left to the user.

Lets see what Lua programmers think on this topic.

Lua programmers are closer to Lisp’s as they have garbage collection mechanism. You don’t have to do manual memory management. On the technical side the algorithm behind Lua’s GC is mark-and-sweep one. In the first phase all accessible objects are marked. During the second step, all non-marked are swept.

You can control the collector by modifying the two parameters:

  • gc pause – controls how much time GC waits before the next cycle
  • gc step multiplier – controls the speed of the collector and indirectly the aggressiveness of releasing the dead objects

Those two control parameters can be controlled by calling the collectgarbage method. This method can be used to obtain information about allocated memory

Running this script will give you the number of allocated bytes by Lua. There are also options to run the GC (I guess if needed) or for example stop the mechanism. The last one is an interesting option I must admit as it might generate some memory pressure situations. Cane be useful sometimes but one need to remember to restart it later.

Looks like Lua offers nice and ‘doing-the-job’ garbage collector.

Founder of Octal Solutions a .NET software house.
Passionate dev, blogger, occasionally speaker, one of the leaders of Wroc.NET user group. Microsoft MVP. Podcaster – Ostrapila.pl