Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 14 · From Scratch · Systems from scratch · about 14 min to read

Play it as a journeyTutor comes with a plan

A garbage collector of your own

How Code Becomes Execution
How Code Becomes Execution · Open board

: the stack empties itself and the heap stays full until someone gives pieces back. This lesson builds the part that finds the pieces nobody needs and gives them back for you.

Before you start. The interpreter in An interpreter of your own keeps names in environments, one per function call, each pointing to its parent, and a function remembers the environment where it was defined. The heap is the work area for anything that must outlive one call (How Code Becomes Execution (compilers, stack & heap)), and A malloc of your own built the part that hands out pieces of it and showed how waste is measured. That allocator trusted the caller to give every piece back. Here nobody has to.

Lesson 112 of 117 in From Scratch

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • April 1960: the program that cleans up after itself
  • What is garbage? An object nothing can reach
  • Roots: where the search for reachable objects starts
  • Mark: walk from the roots and flag everything you reach
  • Sweep: free everything that is not marked
  • The pause: the program stops while this runs
  • Generational: most objects die young
  • The heap
  • Run it
  • Edge cases and trade-offs

Terms it explains: object, garbage, roots, cycle, mark, sweep, mark and sweep, pause, generational, copying, Reference counting, AST, buffer overflow, bytecode, CALL/RET, compiler, dereference (*), GC, heap, interpreter, JIT, linker, memory leak, pointer, segfault, stack, stack frame, stack overflow, use-after-free / dangling pointer.

Review From Scratch