Lua Academy · Lesson

Lua Garbage Collector Basics

How Lua's incremental GC works: mark-and-sweep, thresholds, and collectgarbage().

Lesson 1 of 413 steps

Lua Garbage Collector Basics is a free Lua Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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 use

GC 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)  -- default

Weak 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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Lua Garbage Collector Basics” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Lua Garbage Collector Basics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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

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