# Garbage Collection

URL: https://softwaredictionary.org/terms/garbage-collection
Category: Programming Fundamentals
Last updated: 2026-09-30
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.

## 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 JavaScript

```javascript
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();
```

## Frequently asked questions

**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.

---

Software Dictionary: https://softwaredictionary.org/ · https://softwaredictionary.org/llms.txt
