Advanced Topic 47 of 50 Languages & Compilers
Garbage Collection Algorithms
Mark-sweep, copying and generational collectors.
How it works
Garbage collection algorithms differ in how they find and reclaim unreachable memory. Reference counting keeps a count per object and frees it at zero, but cannot handle cycles without extra work. Mark-and-sweep traces from roots to mark live objects and then sweeps the heap to free the rest; mark-compact also moves live objects together to remove fragmentation; copying collectors such as semi-space copy live objects to a fresh area. Generational collectors segregate young and old objects, and concurrent and incremental collectors reduce pauses with write and read barriers.
Step-by-step walkthrough
- Mark-sweep marks reachable objects, then sweeps unmarked ones.
- It leaves fragmentation.
- Copying collectors move live objects to a new space and free the old one.
- Generational collectors exploit that most objects die young.
- Young collections are quick and old ones rare.
Where it is used
The JVM's G1 and ZGC, Go's concurrent collector, .NET's generational collector and V8's Orinoco use these ideas.
Common pitfall
Stop-the-world pauses hurt latency, and collector overhead consumes CPU and memory, with trade-offs between throughput, pause time and footprint. Reference counting has overhead on every assignment and leaks cycles. Tuning requires understanding allocation patterns. Reduce garbage by reusing objects, and monitor pause times and heap occupancy in production.
Where it fits in the course
Garbage Collection Algorithms is topic 47 of 50 in Languages & Compilers, rated Advanced. It follows Linear and Affine Types. The next topic is Domain-Specific Languages.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Languages & Compilers quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Languages & Compilers quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Garbage Collection Algorithms?
Where do people use Garbage Collection Algorithms?
What is the most common mistake with Garbage Collection Algorithms?
What should I learn before this topic?
How can I practice it?
Is Garbage Collection Algorithms hard to learn?
How long does it take to learn Garbage Collection Algorithms?
Which topics are related to Garbage Collection Algorithms?
What should I study after Garbage Collection Algorithms?
Related topics
- Linear and Affine Types (Languages & Compilers)
- Domain-Specific Languages (Languages & Compilers)
- Dependent Types (Languages & Compilers)
- Operational and Denotational Semantics (Languages & Compilers)
- Line Drawing Algorithms (Graphics, Vision & Media)
- Load Balancing Algorithms (Distributed Systems & Cloud)
- Greedy Algorithms (Algorithms)