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 20 min to read

Play it as a journeyTutor comes with a plan

A malloc 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 hands out the heap, and then measures how it goes wrong.

Before you start. The heap is the work area where a program keeps anything that must outlive one function call (How Code Becomes Execution (compilers, stack & heap)). The kernel gives memory only in whole pages, so the program must cut them into small pieces (The address book every process carries). A first allocator, with first fit, splitting, and merging free neighbours using a size tag at both ends of each piece, was built in Build malloc, then a garbage collector. The file system in A file system of your own tracked free space with a bitmap of equal blocks. Here the pieces have different sizes, so a bitmap is not enough.

Lesson 109 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

  • 28 November 2007: is Firefox’s memory fragmented?
  • The heap: one block of bytes with a road map
  • The free list: the links live inside the free blocks
  • Size bins: one list for each size
  • Measuring: two numbers for waste
  • The arena: allocate many, free all at once
  • heap.py: the allocator
  • bench.py: the workload
  • check.py: does the allocator contradict itself?
  • arena_demo.py
  • What these numbers do not show
  • Real systems

Terms it explains: internal waste, free list, split, merges, size bin, Peak asked, Peak heap, external waste, arena, Common mistakes, dlmalloc, jemalloc, Database buffer pools, 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