garbage collection
The automatic freeing of memory held by objects that the program can no longer reach.
In more detail
It frees programmers from releasing memory by hand and prevents most use-after-free bugs. Collectors either trace which objects are still reachable or count the references to each object, and reclaim the rest.
Examples
CPython uses reference counting plus a cycle collector; freeing a tree mirrors a post-order traversal.In the browser: You never free memory by hand in JavaScript. As long as something reachable still references an object, it stays alive, which is how leaks happen.Go: the garbage collector is linked into every binary as part of the Go runtimeObject a = new Object();
a = null; // the object is now eligibleV8 collects by generation: young objects are swept up often by the fast Scavenge collector, older ones less often by Mark-Sweep-Compact.Garbage collection pauses are one cause of the slow latency tail.Related terms
- heap memory — The pool of memory from which a program allocates objects at run time; objects there can live anywhere and outlive the function that made them.
- post-order traversal — Visiting a node's left subtree, then its right subtree, and the node itself last.
- heap — The area of memory where a program stores objects whose size or lifetime is not known in advance, typically reclaimed by a garbage collector.
- IDisposable — The .NET interface with a single Dispose method, implemented by types that own something the garbage collector cannot clean up, such as a file, socket or database connection.
- .NET runtime — The part of .NET that loads a compiled program, turns its intermediate language into machine code, and manages its memory.
- mark-and-sweep — The garbage collection method that marks every object reachable from the roots, then sweeps away everything unmarked.
- reachability — Whether an object can still be reached by following references from roots such as global variables and the current call stack.
- memory leak — Memory that a program keeps holding although it no longer needs it, so usage grows until the program slows down or crashes.
- WeakMap — A key-value collection whose keys must be objects and are held weakly, so an entry disappears when its key object is garbage collected.
- Go runtime — The support code the compiler builds into every Go program, which schedules goroutines, manages memory and collects garbage.
- memory — The fast, temporary working space where running programs keep their data; it is emptied when the machine powers off.
- pointer — A value that stores the memory location of another piece of data, so the program can follow it to that data.
- JVM — The program that loads class files and runs their bytecode, turning hot code into native machine instructions while it runs.
- Scavenge — V8's fast garbage collection for new space, which copies the few surviving young objects elsewhere and frees everything else at once.
- Mark-Sweep-Compact — V8's full garbage collection for old space: mark every object still reachable, sweep away the rest, then compact memory to close the gaps.
- tail latency — The latency of the slowest requests, measured by high percentiles such as p99.
Taught in
- Binary Trees And Their Traversals — Algorithms & Data Structures for Working Engineers
- Two Copies Of One Service — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Forty One Under-Replicated Partitions — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- What is .NET? Runtime vs SDK, and LTS vs STS — Modern .NET Core, C# & Enterprise Microservices
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- Memory Management: Garbage Collection, Memory Leaks And WeakMap — Frontend Architecture & Web Platform
- What Go Is and Why It Matters — Concurrent Systems Programming with Go
- Sprint: Values And Flow — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Objects, Nested Classes And Garbage Collection — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Node.js Process Architecture: Memory & Heap Spaces — Node.js Internals & Backend Services
- Runtime Configuration: CLI Flags & Native --env-file — Node.js Internals & Backend Services
- Buffer Fundamentals: Slab Allocator & V8 Memory — Node.js Internals & Backend Services
- Buffer Encodings: UTF-8, Hex, Base64 & Slicing — Node.js Internals & Backend Services
- Backpressure Engineering: Handling Mismatched I/O — Node.js Internals & Backend Services
- Asynchronous File I/O: node:fs/promises & Handles — Node.js Internals & Backend Services
- Diagnostic Profiling: CPU Profiler & Heap Snapshots — Node.js Internals & Backend Services
- Why A Histogram Beats An Average — Full-Stack Observability: Metrics, Tracing & Logging
- Latency Numbers Every Engineer Should Know — Scalable System Design & Distributed Architecture
- Observability As A Design Input — Scalable System Design & Distributed Architecture
References
- Python gc: garbage collector interface
- Fundamentals of garbage collection
- MDN: Memory management
- A guide to the Go garbage collector
- Java Garbage Collection Basics (Oracle)
- V8 blog: Trash talk, the Orinoco garbage collector
- MDN Web Docs glossary: Garbage collection