Design a Garbage Collector
🗑️ What is a garbage collector? It is the part of a language runtime that frees memory for you. The program creates objects and links them together, and it never says "delete". The collector works out which objects the program can no longer reach and gives their memory back, so it can be reused for new objects.
Garbage collectors come up in systems programming rounds and in interviews for teams that build runtimes, databases or game engines. The prompt sounds scary, but the core is small: a graph of objects, a set of starting points, and a search. What the interviewer is really testing is whether you can say precisely what "no longer used" means, pick an algorithm that gets it right for every shape of graph (cycles included), and keep the program's API separate from the collection algorithm so the algorithm can change.
Requirements
"Design a garbage collector. A program allocates objects, objects point to each other, and memory that is no longer used should be freed automatically."
We will not manage real memory addresses. Instead we model the heap as a data structure that a program drives through a small API. The questions below turn the vague prompt into rules we can code and test.
Clarifying Questions
You: How does the program talk to us? Do we parse its code, or does it call an API?
Interviewer: An API. The program allocates objects, sets and clears references between them, and tells you which objects it holds directly. Give each object an id.
You: What exactly counts as "no longer used"?
Interviewer: The program holds some objects directly: local variables on the stack and global variables. Call those the roots. Any object you can reach from a root by following references is alive. Everything else is garbage.
You: So two objects that point to each other, but that no root can reach, are garbage?
Interviewer: Yes. Cycles must be freed. That is a big part of the question.
You: When does a collection run?
Interviewer: The program can ask for one at any time. A collection also runs automatically when an allocation does not fit in the remaining memory. If it still does not fit after collecting, the allocation fails.
You: Is a brand new object a root? The program has just received it, so it is holding it.
Interviewer: Good catch. Yes, a new object starts as a root. The program removes the root when its variable goes out of scope.
You: Do we need to move objects around to fight fragmentation?
Interviewer: No. Just free them. Ids never change and are never reused.
You: Does the program keep running while we collect?
Interviewer: Not for now. One thread: when the collector runs, the program waits. We may talk about running them side by side later.
The third answer decides the algorithm. If cycles could be ignored, a simple counter on each object would do. Because they cannot, we need something that looks at the whole graph from the roots. Keep that in mind when we compare the options.
💡 Interview tip: Say the definition of garbage out loud before writing any code: "garbage is anything not reachable from a root". It sounds obvious, but it is the one sentence every later decision rests on, and it tells the interviewer you know the difference between "nobody points to it" and "nobody can reach it".
Final Requirements
- The heap has a fixed capacity. Every object has a size, and the total size of live objects can never exceed the capacity.
allocate(size)creates an object and returns its id. Ids are1, 2, 3, ...in order of successful allocations and are never reused. A new object starts as a root.- If an allocation does not fit, the heap runs a collection first. If it still does not fit, the allocation fails and returns
-1. - The program can add and remove roots, and add and remove references from one object to another. A reference is a directed link; each pair exists at most once, and an object may point to itself.
- An object is alive if it is a root or a live object points to it.
collect()frees every object that is not alive, cycles included, and reports which ones it freed. - Nothing is freed at the moment a root or reference is removed; only a collection frees objects.
- The program can ask whether an object is alive and how much memory is in use.
- The collection algorithm may change later, so it should sit behind its own class.
Out of scope
- Real memory addresses, pointer scanning and object layout.
- Moving or compacting objects (see /low-level-design/memory-allocator for fragmentation).
- Finalizers and weak references (we come back to both in Extensibility).
- A program that runs while the collector runs (also in Extensibility).