Master the fundamental concepts of jvm internals through this focused micro-challenge.
Your toy JVM allocates objects on a simple heap. A mark-and-sweep pass reclaims those unreachable from roots: operand stacks, static fields, and thread-local roots in a real JVM.
Algorithm:
Object headers need a mark bit and a link for the allocation list. Reference fields in objects must be traced during mark.
For example, allocate strings and arrays in \`main\`, drop references, run GC, and print freed versus live counts.
This mirrors the algorithm family behind older stop-the-world JVM collectors before regional designs like G1.
Compacting is optional in your toy collector but fragmentation will show up if you allocate variable-size objects repeatedly. Mark-and-sweep alone is enough to learn root scanning discipline.
This exercise asks you to add mark-and-sweep to your mini-JVM's heap. You will implement root scanning from interpreter stacks and statics, then sweep unreachable objects between bytecode runs.
You will use the same mental model here when reading production interpreter source later in the track. Sketch one concrete input on paper, predict the outcome, then confirm with code. That discipline catches logic errors early and makes debugging far faster when you extend the implementation in follow-on tasks.
Write a C program that adds mark-and-sweep GC to a mini-JVM.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github