Garbage Collection
Garbage collection is an automatic memory management process that identifies and reclaims memory occupied by objects that a program can no longer reach or use, freeing developers from manually deallocating memory.
Definition
Garbage collection is an automatic memory management process that identifies and reclaims memory occupied by objects that a program can no longer reach or use, freeing developers from manually deallocating memory.
Overview
Garbage collection (GC) solves one of the most error-prone problems in software: knowing exactly when a piece of allocated memory is safe to free. In languages without garbage collection, like C, developers must manually call an allocation and deallocation function for every object, and mistakes lead directly to two classes of serious bugs — memory leaks, where memory is never freed even though it's no longer needed, and use-after-free errors, where memory is freed while something still references it. Garbage-collected languages, including Java, Python, JavaScript, and Go, automate this process, tracking which objects are still reachable from the program's active state and reclaiming everything else. Most modern garbage collectors use some variant of tracing, most commonly mark-and-sweep: the collector starts from a set of known 'roots' (global variables, active stack frames) and marks every object reachable from them; anything left unmarked afterward is unreachable garbage and can be safely reclaimed. Generational garbage collectors, used by the JVM and V8 (JavaScript's engine), optimize this further based on the observation that most objects die young — they separate memory into generations and collect the 'young generation' far more frequently than older, longer-lived objects, since scanning the entire heap on every collection would be wasteful. Garbage collection isn't free: a collection cycle takes CPU time and, in some implementations, can pause program execution entirely (a 'stop-the-world' pause), which matters for latency-sensitive applications like real-time systems, high-frequency trading, or games. This has driven ongoing research into concurrent and incremental collectors that minimize pause times, as well as motivating some systems to use manual or reference-counted memory management (like Rust's ownership model or C++'s smart pointers) specifically to avoid GC pauses altogether. Even in garbage-collected languages, memory leaks are still possible — for instance, if a long-lived object (like a global cache or an event listener) holds a reference to objects that are otherwise no longer needed, the garbage collector cannot reclaim them because they're technically still reachable, which is why understanding GC behavior remains important even when you never write explicit deallocation code.
Key Concepts
- Automatically reclaims memory occupied by unreachable objects
- Mark-and-sweep is the most common general tracing algorithm
- Generational collectors optimize by collecting short-lived objects more frequently
- Eliminates most manual memory management bugs like use-after-free
- Can introduce 'stop-the-world' pauses that affect latency-sensitive applications
- Used by Java (JVM), Python, JavaScript (V8), Go, and most managed languages
- Memory leaks are still possible if unreachable-in-practice objects remain referenced