Garbage Collection
- In Turkish
- Çöp toplama
In short
Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
What is garbage collection?
Every value a program creates, from a short string to a large object, takes up memory. Garbage collection is the process by which a language's runtime automatically finds values that are no longer needed and reclaims their memory. Languages such as JavaScript, Java, C#, Python, and Go all use a garbage collector, so developers rarely have to free memory by hand.
Most garbage collectors work by reachability. Starting from roots, such as global variables and the variables of functions that are currently running, the collector follows every reference to find all the objects the program can still reach, and anything it cannot reach is treated as garbage. This approach, called mark-and-sweep, is often combined with generational collection, which checks new objects more often because most objects are only needed briefly.
A garbage collector is like an office cleaning crew that removes anything nobody is using anymore. Employees can focus on their work instead of taking out the trash, but the crew occasionally has to pause activity to do its job. In the same way, garbage collection pauses can cause brief slowdowns, which is why games and other latency-sensitive systems tune their collectors carefully.
Garbage collection is often contrasted with manual memory management in languages like C and C++, where developers call functions such as free themselves and risk crashes and security bugs if they get it wrong. Rust takes a third approach, using ownership rules checked by the compiler to free memory without a garbage collector. Garbage collection also does not prevent every memory leak: if your code keeps references to data it no longer needs, such as in an ever-growing cache or an event listener that is never removed, that data can never be collected.
At a glance
Key takeaways
- Garbage collection automatically frees memory that a program can no longer reach.
- Most collectors trace references from roots; unreachable objects are garbage.
- It prevents many memory bugs but can add short pauses and extra memory overhead.
- Memory leaks still happen when code keeps references it no longer needs.
Example
let user = { name: "Ada", scores: new Array(1_000_000).fill(0) };
let admin = user; // a second reference to the same object
user = null; // still reachable through admin, so it stays in memory
admin = null; // now unreachable: the garbage collector can free it
// A common leak: a cache that only ever grows
const cache = new Map();
function remember(key, value) {
cache.set(key, value); // entries stay reachable forever
}
// A WeakMap holds keys weakly, so entries vanish once a key is unreachable
const metadata = new WeakMap();Readers ask
Which languages use garbage collection?
Most modern high-level languages do, including JavaScript, Java, C#, Python, Go, Ruby, and Kotlin. C and C++ rely on manual memory management, and Rust frees memory automatically through compile-time ownership rules instead of a runtime collector.
Can you have memory leaks with garbage collection?
Yes. The garbage collector only frees memory that is unreachable, so objects that are still referenced, for example by a global variable, an ever-growing cache, or an event listener that was never removed, stay in memory even if the program will never use them again.
Can I force garbage collection in JavaScript?
Not in normal code, because the engine decides when to collect garbage. Node.js can expose a global.gc() function when started with the --expose-gc flag, but it is meant for debugging and testing, not production use.
See also
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- ClosureProgramming Fundamentals, p. 10A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.
- CompilerProgramming Fundamentals, p. 11A compiler is a program that translates source code written in a programming language into a lower-level form, such as machine code, that a computer can run.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- Node.jsBackend & APIs, p. 32Node.js is an open-source JavaScript runtime that runs JavaScript outside the browser, most often to build web servers, APIs, and command-line tools.
- Heap MemoryOperating Systems, p. 14Heap memory is the region for data a program allocates at runtime, whose size or lifetime isn't known in advance and can outlive the function that made it.
- JVMProgramming Languages, p. 17The JVM runs Java bytecode, so the same compiled program works on any operating system; Kotlin, Scala and Clojure compile to that bytecode too.
Spotted a mistake or something missing on this page?Suggest an edit