Lua Garbage Collector Basics
How Lua's incremental GC works: mark-and-sweep, thresholds, and collectgarbage().
Lua Garbage Collector Basics is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Garbage Collection?
Lua uses an automatic garbage collector to reclaim memory occupied by objects (tables, strings, closures) that are no longer reachable from any root.
Mark-and-Sweep Algorithm
Lua's GC uses an incremental mark-and-sweep algorithm. It marks all reachable objects starting from roots, then sweeps (frees) unmarked objects.
Incremental Collection
The GC runs in small increments interleaved with the program. This avoids long pause times. The amount of work per increment is controlled by the GC step parameters.
collectgarbage Function
collectgarbage(opt, arg) controls the GC directly.
collectgarbage("collect") -- run a full GC cycle
collectgarbage("stop") -- stop automatic GC
collectgarbage("restart") -- restart automatic GC
print(collectgarbage("count")) -- KB of memory in useGC Parameters
collectgarbage("setpause", n) sets the pause between GC cycles (200 = double memory before next cycle). "setstepmul" controls GC speed.
collectgarbage("setpause", 200) -- default
collectgarbage("setstepmul", 200) -- defaultWeak References and GC
Normally a reference keeps an object alive. A weak reference does not — the GC can collect the object even if a weak table points to it.
Generational GC (Lua 5.4)
Lua 5.4 introduced an optional generational GC mode. Young objects are collected frequently; old objects less often. Toggle with collectgarbage("generational").
Memory Pressure Triggers
The GC runs automatically when memory usage exceeds a threshold (set by pause). Allocating many temporary tables increases GC pressure and can slow your program.
Reducing GC Pressure
- Reuse tables instead of allocating new ones.
- Avoid creating many short-lived closures in hot loops.
- Pre-allocate known-size arrays.
String Interning
Lua interns all strings — identical strings share one object. This means string creation is O(n) hash but comparison is O(1). Short strings are very cheap.
Finalizers (__gc)
Tables can have a __gc metamethod called by the GC before the object is freed. Useful for releasing external resources like file handles or C pointers.
GC Question
What does collectgarbage("count") return?
Recap: Lua GC Basics
Lua's incremental mark-and-sweep GC automatically reclaims unreachable memory. Use collectgarbage to monitor and control collection. Reduce pressure by reusing tables and avoiding excessive allocations in hot paths.
Frequently asked questions
Is the “Lua Garbage Collector Basics” lesson free?
Yes — the full text of “Lua Garbage Collector Basics” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Lua Garbage Collector Basics”?
How Lua's incremental GC works: mark-and-sweep, thresholds, and collectgarbage(). You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Lua Garbage Collector Basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Lua Garbage Collector Basics
- Weak Keys and Weak Values
- Finalizers with __gc
- Avoiding Memory Leaks