Skip to main content

Garbage Collection

In Turkish
Çöp toplama
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/garbage-collection

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

Mark and sweep: starting from the roots, the variables still in use, the garbage collector follows references and marks objects A, B and C as reachable. Objects D and E only point at each other, nothing in use reaches them, so they are swept and their memory is freed.Rootsvariables in useABCreachable: keptDEunreachable: freed1Mark: follow every reference from the roots2Sweep: free every object that wasn't marked
The collector doesn't ask what you will use; it keeps whatever can still be reached from the roots and frees the rest, even objects that point at each other.

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

Reachability and a common leak in JavaScriptjavascript
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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings