The first chapter of this level introduced the three ways to run a program: compile it, interpret it, or compile it just in time. This one opens the interpreter. Languages like Java, Python, JavaScript and C# run on a runtime, a program that provides two things C leaves to you: a virtual machine that executes bytecode, and a garbage collector that frees memory automatically. Both are ordinary programs, and both fit in a few dozen lines of C at their simplest.
A virtual machine on one page of C
A bytecode VM is a loop: fetch the next bytecode instruction, decode it, execute it, repeat. It's the fetch-decode-execute cycle of a real CPU, done in software. Here is one, with its own operand stack and local variables, running a program in real JVM bytecode. The instructions come from IJVM, a small teaching subset of Java bytecode with the same opcode numbers as the Java Virtual Machine: 0x10 is BIPUSH, 0x60 is IADD, 0xA7 is GOTO.
The bytecode, assembled by hand, computes the same function as the first chapter's demo: s = 0; for (i = 0; i != n; i++) s += i * 2; return s;, with i * 2 written as i + i, since IJVM has no multiply.
Try it: Press Next line to run the highlighted line, or Play to watch; the boxes on the right are the variables, and the ones that just changed light up. Edit the code to try your own changes.
1// total(n): s = 0; for (i = 0; i != n; i++) s += i + i; return s;2unsigned char code[] = {3 0x10, 0, // 0 BIPUSH 04 0x36, 1, // 2 ISTORE 1 s = 05 0x10, 0, // 4 BIPUSH 06 0x36, 2, // 6 ISTORE 2 i = 07 0x15, 2, // 8 ILOAD 28 0x15, 0, // 10 ILOAD 09 0x9f, 0x00, 18, // 12 IF_ICMPEQ +18 i == n: go to 3010 0x15, 1, // 15 ILOAD 111 0x15, 2, // 17 ILOAD 212 0x59, // 19 DUP13 0x60, // 20 IADD i + i14 0x60, // 21 IADD s + (i + i)15 0x36, 1, // 22 ISTORE 116 0x84, 2, 1, // 24 IINC 2 1 i++17 0xa7, 0xff, 0xed, // 27 GOTO -19 back to 818 0x15, 1, // 30 ILOAD 119 0xac // 32 IRETURN20};2122int run(int n) {23 int stack[8];24 int local[4];25 int sp = 0;26 int pc = 0;27 local[0] = n;28 while (1) {29 int op = code[pc];30 switch (op) {31 case 0x10: // BIPUSH byte32 stack[sp++] = (signed char)code[pc + 1];33 pc += 2;34 break;35 case 0x15: // ILOAD varnum36 stack[sp++] = local[code[pc + 1]];37 pc += 2;38 break;39 case 0x36: // ISTORE varnum40 local[code[pc + 1]] = stack[--sp];41 pc += 2;42 break;43 case 0x59: // DUP44 stack[sp] = stack[sp - 1];45 sp++;46 pc += 1;47 break;48 case 0x60: // IADD49 sp--;50 stack[sp - 1] = stack[sp - 1] + stack[sp];51 pc += 1;52 break;53 case 0x84: // IINC varnum const54 local[code[pc + 1]] += (signed char)code[pc + 2];55 pc += 3;56 break;57 case 0x9f: // IF_ICMPEQ offset58 sp -= 2;59 if (stack[sp] == stack[sp + 1])60 pc += (short)(code[pc + 1] << 8 | code[pc + 2]);61 else62 pc += 3;63 break;64 case 0xa7: // GOTO offset65 pc += (short)(code[pc + 1] << 8 | code[pc + 2]);66 break;67 case 0xac: // IRETURN68 return stack[--sp];69 }70 }71}7273int main() {74 return run(5);75}
Press To the end to run it: it returns 20, the same answer as the compiled version. Then restart and step through a few iterations of the while loop, watching the boxes for pc, sp, stack and local: the VM has its own program counter, stack pointer and registers, kept in ordinary memory. The real CPU is running the interpreter; the interpreter is running the bytecode.
Everything here mirrors the hardware. Branch offsets are signed 16-bit numbers relative to the branch instruction itself, so GOTO -19 at address 27 jumps back to 8, and (short)(hi << 8 | lo) decodes them big-endian, as the JVM specifies. The Mic-1, a classic teaching microarchitecture, interprets exactly this instruction set, in microcode instead of C: the microprogramming chapter shows how it dispatches on the opcode byte in a single step, where this switch needs a chain of compares.
What interpretation costs
The bytecode program executes 64 bytecode instructions. The simulated CPU, running the interpreter, executes about 3,100 machine instructions to get there: close to 50 machine instructions per bytecode, here with an unoptimized compiler. The same total(5) compiled directly to machine code took 81 instructions in the first chapter.
The overhead comes from work the compiled version never does: fetching each opcode from memory, decoding it through the switch, keeping stack, local, sp and pc in memory instead of registers, and jumping back to the top of the loop. Real interpreters are written much more carefully than this one, and compiled with optimizations, so the ratio shrinks a lot. But it never reaches zero, which is why the gap between CPython and C in the first chapter was about 20×, and why JIT compilers exist.
Stack machines and register machines
This VM is a stack machine: IADD takes no operands, because they're always the top two stack entries. The JVM, CPython, WebAssembly and .NET all work this way. The bytecode is compact and simple to generate, but it needs many instructions, since every value must be pushed before use.
A register machine names its operands instead, like a real CPU: Lua 5 and Android's Dalvik VM use instructions like ADD r1, r2, r3, and V8's Ignition is a register machine with an accumulator (Ldar, Star in the first chapter's listing). Each instruction is bigger, but there are fewer of them, and fewer dispatches. For an interpreter, dispatch is the dominant cost.
Dispatch
The switch in this VM has nine cases spread from 0x10 to 0xAC, too sparse for a jump table, so the compiler emits a chain of up to nine compares for each bytecode: the control-flow chapter showed why. Real VMs use dense opcode numbers, 0 to about 200, so their switch becomes a jump table: one indirect jump per bytecode.
The next step is threaded code: instead of jumping back to a single switch at the top of the loop, each instruction's handler ends with its own indirect jump to the next handler. CPython does this when the compiler supports "computed goto" (a GCC extension, also in clang). One indirect jump per handler, instead of one shared jump for all of them, gives the branch predictor much better odds: it can learn that ILOAD is often followed by another ILOAD. CPython 3.14 added an optional interpreter built on tail calls between handlers, for the same reason.
Garbage collection
The heap chapter ended with the bugs of manual free: leaks, use-after-free, double free. A garbage collector removes them by making freeing automatic: the runtime frees an object once the program can no longer reach it. There are two families of approach.
Reference counting
Each object keeps a count of the references pointing to it. Every assignment updates the counts, and when a count drops to zero, the object is freed immediately. CPython works this way, and you can see the count:
>>> a = Node()
>>> sys.getrefcount(a) # a, plus getrefcount's own argument
2
>>> b = a
>>> sys.getrefcount(b)
3
Reference counting frees memory as early as possible, and predictably. But it costs work on every assignment, and it has a blind spot: cycles. If x points to y and y points back to x, both counts stay at 1 even after the program drops them, and they're never freed. CPython therefore also runs a separate cycle collector: after x.other = y; y.other = x; del x, y, a call to gc.collect() reports 2 unreachable objects found and freed. Swift and C++'s shared_ptr use reference counting too, and leave cycles to the programmer, through weak references.
Tracing: mark and sweep
A tracing collector takes the opposite view. It doesn't track assignments at all. From time to time, it starts from the roots (global variables, and the variables on every thread's stack) and follows every pointer, marking each object it reaches. Anything unmarked is garbage, however it's connected, cycles included. Then it sweeps: walks the whole heap and frees the unmarked objects.
Here is a complete mark-and-sweep collector over a heap of eight objects. The program builds A → B → C, plus two objects D and E that point to each other, then drops its references to C, D and E:
Try it: Press Next line to run the highlighted line, or Play to watch; the boxes on the right are the variables, and the ones that just changed light up. Edit the code to try your own changes.
1struct Obj {2 int used;3 int marked;4 struct Obj *a;5 struct Obj *b;6};78struct Obj heap[8];9struct Obj *root;1011struct Obj *new_obj(void) {12 for (int i = 0; i < 8; i++) {13 if (!heap[i].used) {14 heap[i].used = 1;15 heap[i].a = 0;16 heap[i].b = 0;17 return &heap[i];18 }19 }20 return 0;21}2223void mark(struct Obj *o) {24 if (o == 0 || o->marked)25 return;26 o->marked = 1;27 mark(o->a);28 mark(o->b);29}3031int sweep(void) {32 int freed = 0;33 for (int i = 0; i < 8; i++) {34 if (heap[i].used && !heap[i].marked) {35 heap[i].used = 0;36 freed++;37 }38 heap[i].marked = 0;39 }40 return freed;41}4243int main() {44 root = new_obj(); // A45 root->a = new_obj(); // A -> B46 root->a->a = new_obj(); // B -> C47 struct Obj *x = new_obj(); // D48 struct Obj *y = new_obj(); // E49 x->a = y; // D -> E50 y->a = x; // E -> D: a cycle51 x = 0;52 y = 0; // nothing points to D or E any more, except each other53 root->a->a = 0; // and nothing points to C54 mark(root);55 return sweep();56}
It returns 3: C, D and E are freed. Marking starts at root, reaches A and B, and stops, so the D ↔ E cycle is never marked, which reference counting could never have concluded. Watch the used and marked fields of the heap array, among the global variables, as mark and sweep run. mark is recursive, so it's also a good place to watch the calls pile up on the stack.
Real collectors
Production collectors add three ideas to this skeleton:
- Moving and compacting. Instead of leaving holes, a copying collector moves every live object into a fresh region, packed together, and updates every pointer to it. Allocation then becomes a pointer increment, as fast as a stack.
- Generations. Most objects die young: temporary strings, iterator objects, intermediate results. Generational collectors allocate new objects in a small young area and collect it often and cheaply, and only occasionally collect the old generation. V8 shows it with
node --trace-gc: a loop that allocates 2 million short-lived objects triggers 41 Scavenge collections of the young generation, each taking 0.1 to 0.5 ms, and no full Mark-Compact of the whole heap at all. - Concurrency. Stopping every thread while the heap is traced gives pause times proportional to the heap. Modern collectors do most of the marking concurrently with the program. The JVM's default collector, G1, targets pauses of a few hundred milliseconds or less, and ZGC keeps them under a millisecond, even on heaps of many gigabytes.
The trade-off is the same as everywhere in this level: garbage collection costs CPU time and memory headroom, and in exchange the program can't leak a forgotten object or use one after it's freed.
Takeaways
- A bytecode VM is a fetch-decode-execute loop in software, with its own program counter, stack and locals. The demo runs real JVM (IJVM) opcodes and returns the same 20 as the compiled code.
- Interpreting costs dozens of machine instructions per bytecode (about 50 here) for fetching, decoding, dispatch and keeping VM state in memory. JITs exist to remove that overhead.
- Stack VMs (JVM, CPython, WebAssembly) have compact bytecode; register VMs (Lua, Dalvik, V8's Ignition) execute fewer instructions. Dispatch is optimized with jump tables and threaded code.
- Reference counting frees objects as soon as their count hits zero, but can't free cycles on its own.
- Tracing collectors mark everything reachable from the roots and sweep the rest, cycles included.
- Real collectors compact, split objects into generations, and run concurrently to keep pauses short.