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 runtime
Object a = new Object();
a = null; // the object is now eligible
V8 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

Taught in

References

Certification courses