Master the fundamental concepts of garbage collection through this focused micro-challenge.
Reference counting increments on alias creation, decrements on drop. At zero, recurse into children. Swift ARC and Python's primary mechanism use refcounting. Cycles (a->b->a) need a separate cycle breaker or weak refs. Objective-C and COM also built industrial-strength systems on refcounting before tracing GC went mainstream.
inc(obj): header count++. dec(obj): count--; if zero, free children then object. Combine with cycle detection (Python's gc module) for completeness. Thread-safe refcounting uses atomic increment and decrement on every pointer copy.
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 reference counting garbage collection. This exercise requires increment and decrement on pointer operations and recursive freeing when counts reach zero.
Implement reference counting 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