Garbage collection basics in Compiler Design - Time & Space Complexity
We want to understand how the time cost of garbage collection changes as the program's memory usage grows.
How does the work of cleaning unused memory scale with more objects?
Analyze the time complexity of the following garbage collection process.
// Simple mark-and-sweep garbage collector
function garbageCollect(heap) {
for (const object of heap) {
object.marked = false;
}
markRoots();
for (const object of heap) {
if (!object.marked) {
free(object);
}
}
}
function markRoots() {
for (const root of roots) {
mark(root);
}
}
function mark(object) {
if (object.marked) return;
object.marked = true;
for (const child of object.references) {
mark(child);
}
}
This code marks all reachable objects starting from roots, then frees unmarked objects.
Look for loops and recursion that repeat work.
- Primary operation: Traversing all objects and their references to mark reachable ones.
- How many times: Each object is visited once during marking; then all objects are checked once during sweeping.
As the number of objects (n) grows, the collector must check and mark more objects.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 (mark + sweep) |
| 100 | About 200 |
| 1000 | About 2000 |
Pattern observation: The work grows roughly in direct proportion to the number of objects.
Time Complexity: O(n)
This means the time to collect garbage grows linearly with the number of objects in memory.
[X] Wrong: "Garbage collection time stays the same no matter how many objects exist."
[OK] Correct: The collector must visit each object to check if it is still used, so more objects mean more work.
Understanding how garbage collection time grows helps you explain performance impacts in real programs and shows you can reason about resource management.
"What if the garbage collector used a generational approach that only checks new objects? How would the time complexity change?"
Practice
Solution
Step 1: Understand what garbage collection does
Garbage collection automatically frees memory that the program no longer uses, preventing memory waste.Step 2: Compare options with this purpose
Only To automatically free memory that is no longer used by the program describes this automatic freeing of unused memory, while others describe unrelated tasks.Final Answer:
To automatically free memory that is no longer used by the program -> Option AQuick Check:
Garbage collection = automatic memory freeing [OK]
- Thinking garbage collection speeds up program execution
- Confusing garbage collection with compiling
- Believing garbage collection stores data permanently
Solution
Step 1: Recall how garbage collection works in modern languages
Most modern languages run garbage collection automatically without needing explicit calls or syntax.Step 2: Evaluate each option
Options B, C, and D suggest manual calls or commands that are not standard syntax; only A correctly states automatic operation.Final Answer:
Garbage collection runs automatically without explicit syntax -> Option CQuick Check:
Garbage collection = automatic, no manual calls [OK]
- Assuming you must call garbage collection manually
- Looking for special syntax to start garbage collection
- Confusing garbage collection with memory free functions
var a = new Object() var b = a b = null // Garbage collection runs here
Which object(s) will be collected by garbage collection?
Solution
Step 1: Analyze references to the object
Initially, bothaandbpoint to the same object. Afterb = null, onlyareferences it.Step 2: Determine if object is unreachable
Sinceastill references the object, it is reachable and will not be collected.Final Answer:
No objects will be collected becauseastill references the object -> Option AQuick Check:
Object referenced = not collected [OK]
- Thinking object is collected when one reference is null
- Confusing variable collection with object collection
- Assuming variables themselves are garbage collected
var x = new Object() var y = x x = null // Garbage collection runs here print(y.property)
What problem might occur?
Solution
Step 1: Check references after
Even thoughx = nullxis null,ystill references the object, so it remains reachable.Step 2: Understand garbage collection behavior
Garbage collection only frees objects with no references. Sinceyreferences the object, it won't be freed, soy.propertyis safe.Final Answer:
No error; object is still referenced byy-> Option DQuick Check:
Object referenced = no error [OK]
- Assuming object is freed when one reference is null
- Expecting syntax error for missing manual garbage call
- Confusing variable null assignment with object deletion
Solution
Step 1: Understand reachability in garbage collection
Garbage collection frees objects unreachable from any external references, even if they reference each other.Step 2: Apply this to cyclic references
Since no external variables reference the cycle, the entire cycle is unreachable and will be collected.Final Answer:
The cycle of objects will be collected because they are unreachable from outside -> Option BQuick Check:
Unreachable cycles = collected [OK]
- Believing cycles prevent garbage collection
- Thinking only some objects in cycle are collected
- Expecting runtime errors from cycles
Ready to prove it?
Pass the test and this pattern moves unknown
