0Pricing
Lua Academy · Lesson

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

  1. Lua Garbage Collector Basics
  2. Weak Keys and Weak Values
  3. Finalizers with __gc
  4. Avoiding Memory Leaks
← Back to Lua Academy