Lua Garbage Collector Basics
How Lua's incremental GC works: mark-and-sweep, thresholds, and collectgarbage().
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.
All lessons in this course
- Lua Garbage Collector Basics
- Weak Keys and Weak Values
- Finalizers with __gc
- Avoiding Memory Leaks