Tiny Garbage Collector for C is implemented in roughly 500 lines of code. It uses a conservative, thread-local mark-and-sweep strategy to reclaim unreachable allocations automatically. Memory allocated through its API can still be manually released when deterministic cleanup is required. Developers can register destructors that run immediately before managed objects are freed. Garbage collection normally runs after allocation thresholds are reached, but it can also be triggered, paused, or resumed manually. Special root allocations can be preserved even when they are not reachable through the usual stack or managed-memory rules. Its small codebase makes tgc useful for embedded experiments, language runtimes, and learning how garbage collectors work.
Features
- Thread-local memory management
- Automatic unreachable allocation cleanup
- Manual collection and freeing
- Conservative mark-and-sweep collection
- Registered destructors
- Persistent root allocations