# Ch 26: Garbage Collection

> Study guide to Crafting Interpreters chapter 26. A mark-sweep garbage collector: roots, marking with a gray stack (tricolor abstraction), tracing…

Updated October 20, 2026.

## Summary {#summary}

Crafting Interpreters chapter 26, Garbage Collection: A mark-sweep garbage collector: roots, marking with a gray stack (tricolor abstraction), tracing references, sweeping unreachable objects, weak references for the string table, and when to trigger collection. Budget about 8 hours; the book's `chap26_garbage` tests check your code.

## Key facts {#key-facts}

- Week 24 of 145 on the cracked.guide path, in [Crafting Interpreters](https://cracked.guide/crafting-interpreters).
- Time budget: about 8 hours.
- Part of the book: clox (bytecode VM, C).
- Verified on cracked.guide when the official `chap26_garbage` tests pass on GitHub Actions.
- 3 challenges at the end of the chapter.
- Terms it teaches: [clox](https://cracked.guide/glossary/clox), [Mark-sweep GC](https://cracked.guide/glossary/mark-sweep-garbage-collection).

## Week 24 goals {#objectives}

By the end of week 24 you can:

- Explain reachability, roots and the tricolor abstraction.
- Implement mark-sweep collection: mark roots, trace references, sweep white objects.
- Use stress-GC mode to expose objects collected too early.

## This week's steps {#steps}

- Read the chapter: Enable the stress-GC mode (collect on every allocation) and the GC log as the book suggests.
- Write the code in your lox repo: Type it into clox/ (C) yourself as you read, and get it running.
- Do the challenges (1–3): Challenge 1 (shrink the object header) and challenge 2 (avoid clearing mark bits on every sweep) are good optimizations.
- Push and let the tests verify it: Your repo runs the book's chap26_garbage tests on every push; passing marks this done.

## Why it matters {#why}

GC is one of the most misunderstood parts of every runtime. After this, Java, Go and JavaScript GC logs make sense, and Knuth 2.3.5 will feel familiar.

## How to approach it {#approach}

Enable the stress-GC mode (collect on every allocation) and the GC log as the book suggests. Most bugs only show up under stress mode.

## Challenges {#challenges}

3 challenges at the end of the chapter.

Challenge 1 (shrink the object header) and challenge 2 (avoid clearing mark bits on every sweep) are good optimizations. Challenge 3 (try another GC algorithm) is a big project; save it for after the book.

## Common traps {#traps}

- An object allocated but not yet reachable gets collected mid-operation. Push temporaries on the stack before allocating again.
- Forgetting to mark compiler roots while compiling.
- Not removing unmarked strings from the intern table before sweeping.

## Links {#links}

- [Read the chapter: Ch 26: Garbage Collection](https://craftinginterpreters.com/garbage-collection.html)
- [Official repo: book source, both interpreters, test suite](https://github.com/munificent/craftinginterpreters)
- [Running the official test suite against your own interpreter (repo README)](https://github.com/munificent/craftinginterpreters#testing-your-implementation)
- [Wikipedia: Tracing garbage collection](https://en.wikipedia.org/wiki/Tracing_garbage_collection)
- [Hacker News: Ruby Garbage Collection Deep Dive: Tri-Color Mark and Sweep](https://news.ycombinator.com/item?id=26182796)

---

Source: https://cracked.guide/guide/ci-26
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
