Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Garbage collector

Сборщик мусора

A runtime component that automatically reclaims memory occupied by objects the program can no longer reach.

Also known as: GC, garbage collection

What a garbage collector does

When a program creates an object in managed memory, it doesn't have to decide exactly when to free that object's storage. The garbage collector (GC) identifies objects that can no longer be reached and makes their space available for reuse. Garbage collection is the process; garbage collector is the component that performs it.

How reachability works

Think of objects as a graph, with references connecting them. A tracing collector starts from a set of known entry points called GC roots. These can include references held by active function calls and references in static fields. It follows strong references outward. If there's no such path from a root to an object, that object is eligible for collection. A cycle is not a problem on its own: two objects can point to each other and still be disconnected from every root.

Mark-and-sweep is one way to implement this idea: mark reachable objects, then reclaim the space occupied by unmarked ones. Some collectors also move live objects to reduce gaps in the heap. Generational collectors take advantage of the fact that many objects die young: they collect recently allocated objects more often instead of examining the whole heap each time.

Example: clearing a variable isn't enough

const cache = new Map();
let profile = { name: 'Ada' };
cache.set('current', profile);
profile = null;
cache.delete('current');

Setting profile to null removes one reference, not the object. As long as the reachable map holds the entry, the object stays reachable through the map. Deleting the entry makes the object eligible for collection if no other references keep it alive. It doesn't schedule a collection at that line.

What GC changes in practice

The JVM, .NET, Go, and JavaScript engines all use garbage collection, but they don't all collect in the same way. A collector can pause application execution during some phases and work alongside the application during others. For a service, both pause duration and the CPU cost of collection matter. Returning space to the managed heap also doesn't necessarily reduce the process's memory footprint right away.

Automatic collection doesn't prevent memory leaks. An unbounded cache can keep old values reachable long after the application stops using them. Nor does GC give you predictable cleanup for resources such as sockets or open files. Close those explicitly or use the language's resource-management constructs, such as using in C#.

How it comes up in interviews

You may be asked to trace an object graph or explain why memory keeps growing in an application that has GC. Distinguish three questions: Is the object still reachable? Has the collector reclaimed its space? Has the runtime returned any memory to the operating system? Those events don't happen at the same time.