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 06 · How Computers Work · Build the machine's software · with code: Go · about 21 min to read

Play it as a journeyTutor comes with a plan

Build malloc, then a garbage collector

How Code Becomes Execution
How Code Becomes Execution · Open board

The two work areas a running program keeps: a stack that empties itself when a call returns, and a heap where things stay until someone says they are done. This lesson builds the part that hands out the heap and takes it back.

Before you start. A running program keeps two work areas in memory. The stack holds calls that have not finished, and empties itself as they return. The heap holds anything that must outlive the call that made it, and keeps it until the program gives it back. Forgetting to give it back is a leak. Using it after giving it back is a use-after-free (How Code Becomes Execution (compilers, stack & heap)). The kernel, the one program in charge of the hardware, hands out memory only in whole pages of several kilobytes, so something inside the program must cut pages into the small pieces it asks for (The address book every process carries). The first part of this build wrote a computer as a loop in Go (Build a stack machine). This part needs Go too, and nothing else from that one.

Lesson 38 of 41 in How Computers Work

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

  • 1987: the programs that ran slow and fat
  • A chalk line moving right: the allocator
  • Gaps too small to use: fragmentation
  • A label at each end: the boundary tag
  • Nobody says done: the garbage collector, and its roots
  • The real ones: the same clerk, faster

Terms it explains: allocator, fragmentation, boundary tag, garbage collector, roots, 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 How Computers Work