Master the fundamental concepts of garbage collection through this focused micro-challenge.
Managed runtimes reclaim unreachable objects automatically. Mark-and-sweep: trace from roots (stack, globals), mark reachable objects, sweep unmarked objects back to the free list. Python's cyclic GC layers atop refcount; Go uses concurrent mark-sweep.
Mark: DFS/BFS from roots, set mark bit in object header. Sweep: walk heap, free unmarked objects, clear marks on survivors for next cycle.
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 basic mark-and-sweep garbage collection. This exercise requires tracing from roots, marking reachable heap objects, and reclaiming unmarked memory in a sweep pass.
Implement basic mark-and-sweep garbage collector.
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