Garbage Collection Pauses
Why programs written in languages that automatically manage memory can suddenly freeze for milliseconds at unpredictable moments, and why that's a serious problem for latency-sensitive trading systems.
Prerequisites: System Calls and Context Switches
Languages like Java, C#, and Python manage memory for you: when your program creates objects, the runtime automatically reclaims the memory of ones no longer in use, via a process called garbage collection. This is convenient — no manual memory bookkeeping — but it isn't free. To safely reclaim memory, many garbage collectors need to briefly pause the entire program (a "stop-the-world" pause) so nothing changes underneath them while they scan and clean up.
For most software these pauses, often just a few milliseconds, are invisible. For a trading system quoting or reacting to market data on microsecond-to-millisecond timescales, a multi-millisecond freeze at an unpredictable moment can mean missing a price update, quoting a stale price into a fast market, or losing a race to cancel an order before it gets hit. The pause isn't just slow, it's unpredictable — it can strike in the middle of the most sensitive moment of the trading day.
Worked example
A market-making engine written in Java targets sub-millisecond response times. During a volatile open, the garbage collector triggers a 15-millisecond stop-the-world pause exactly as prices are gapping. The engine's stale quotes sit resting in the book for those 15 milliseconds and get picked off by faster participants — a loss directly attributable to the pause, not to any flaw in the trading logic itself. Firms respond by tuning collector settings, pre-allocating memory to avoid triggering collection during trading hours, or avoiding garbage-collected languages entirely in the hottest paths.
Garbage collection pauses freeze an entire program unpredictably while memory is reclaimed, and even a few milliseconds of that at the wrong moment can be costly for latency-sensitive trading systems — which is why hot-path trading code is often written to minimize object allocation or avoid garbage-collected languages altogether.
Related concepts
Practice in interviews
Further reading
- Oracle, 'Java Garbage Collection Basics'