Master the fundamental concepts of garbage collection through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksMost objects die young. Split heap into young (nursery) and old generations. Minor collections copy survivors with copying GC; promote long-lived objects to old gen. Major collections run rarely. HotSpot's G1 and .NET GC extend this model.
Allocate in nursery until full. Minor GC copies survivors to survivor space or promotes. Old gen collected less often with mark-sweep or mark-compact.
cLoading…
Production compilers embed this step inside a longer pipeline. GCC flows through cpp, cc1, assembly, and ld; Clang uses the driver, Sema, LLVM IR passes, and a target backend. LLVM bitcode, JVM bytecode, and WASM are other familiar IRs at the same layer. The exercise isolates one pass so you can test it alone before chaining it to the next stage.
You will implement generational garbage collection with nursery and old generation. This exercise requires a fast minor collection, promotion policy, and write barriers for cross-generation pointers.
Implement a generational collector: new objects live in a small nursery that is collected often and cheaply (minor GCs); objects that survive long enough are promoted to the old generation, which is collected rarely (major GCs). A remembered set of old→young pointers lets a minor GC skip scanning the old generation entirely.
| Command | Meaning |
|---|---|
config nursery BYTES promote AGE oldlimit BYTES | first line |
new NAME SIZE | allocate in the nursery; if it doesn't fit, run a minor GC first |
link A B / unlink A B | ordered pointers, as in the mark-and-sweep task |
root NAME / unroot NAME | roots, in order |
minor / major | run a collection now |
link A B with A old and B young adds A to the remembered set.AGE is promoted to old. Then rebuild the remembered set: every old object (in allocation order) that still points to a young object.oldlimit bytes, a major GC runs automatically.cLoading…
Minor and major GCs are numbered separately. Lists are in allocation order and print none when empty. At the end: young: NAME(age) ... and old: NAME ... (or none).
Input:
cLoading…
Output:
cLoading…
Hidden tests cover floating garbage (a dead old object keeping a young one alive until a major GC), an allocation that triggers a minor GC, the old-generation limit triggering a major GC, and unrooted young objects freed by a minor GC.