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 worksMinor GC scans nursery but must also scan old objects pointing into nursery. Tracking every old object is expensive. Write barriers log stores of old->young pointers into a remembered set or card table. HotSpot and Go use variants.
After old.field = young, barrier marks card containing old as dirty or logs the slot. Minor GC roots include remembered set entries.
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 write barriers for generational GC. This exercise asks you to instrument reference stores and maintain a remembered set so minor collections find all nursery pointers.
Implement the two classic write barriers a generational GC uses to find old→young pointers without scanning the whole old generation, and compare their costs: card marking (cheap barrier, coarse scan) and a precise remembered set of slots (costlier barrier, exact scan).
cLoading…
Only old objects receive stores in these scripts. A slot is identified by its address; storing to it again overwrites its value.
(Young objects are not moved or promoted in this model.)
For card mode each store prints store 0xSLOT -> TARGET: card N dirty; for remset mode, store 0xSLOT -> TARGET: remembered or store 0xSLOT -> TARGET: ignored. Each minor prints:
cLoading…
(cards none if no dirty card; young roots: none if empty; roots listed in slot order, each name once.) Finally barrier work=B scan work=S total=T.
Input:
cLoading…
Output:
cLoading…
Hidden tests compare both modes on the same stores, include overwritten slots and null stores, and run several minor collections.