Course contentsShow
← Computer Science
Lesson 2100 of 2,87247. Memory Management in LanguagesFree lesson

Stop-the-World vs Incremental Collection

Stop-the-world pauses all threads; incremental GC interleaves collection with program execution.

Stop-the-World vs Incremental Collection

You'll learn the fundamental difference between garbage collection that pauses your entire program versus collection that runs alongside it.

The Core Tradeoff

When a garbage collector runs, it faces a critical decision: should it pause the entire program to do its work cleanly, or should it try to collect garbage while the program continues running?

Stop-the-World (STW) collection halts all application threads during garbage collection. Every thread freezes while the collector marks reachable objects and reclaims memory. This ensures the heap remains consistent—no objects move or change reachability while the collector works. It's like closing a store completely to do inventory: accurate, but customers must wait.

Incremental collection interleaves garbage collection work with normal program execution. The collector performs small chunks of work between program operations, allowing threads to continue running. Think of it like a cashier who stocks shelves between customers—the store stays open, but the work takes longer overall.

Why It Matters

STW pauses can cause noticeable application freezes, especially problematic for real-time systems, video games, or user interfaces where responsiveness matters. A 100ms STW pause might be invisible in a batch job but catastrophic for a 60fps game.

Incremental collection avoids long pauses but introduces complexity: the program might modify objects the collector just examined (requiring write barriers) and overall throughput often decreases because the collector must track concurrent changes.

Many modern collectors use hybrid approaches—mostly incremental with short STW pauses for critical phases like root scanning.

Key Takeaway: Stop-the-world collection pauses all threads for simple, fast collection; incremental collection spreads work across execution to minimize pause times at the cost of complexity and throughput.