Glossary termMemory management technique

Garbage Collection

Also known as: GC, Automatic memory management

Automatic memory management in which a language runtime finds and reclaims memory occupied by objects a program can no longer use.

Garbage collection is a form of automatic memory management. Programs constantly create objects, such as strings, lists and records, that occupy memory. In languages without garbage collection, such as C, the programmer must explicitly free each piece of memory once it is no longer needed. A garbage collector does this automatically: it periodically determines which objects can no longer be reached from the running program, through any chain of references starting from the variables in use, and reclaims their memory for reuse.

John McCarthy invented garbage collection around 1959 to simplify memory management in Lisp. Two broad families developed. Tracing collectors, such as mark-and-sweep, start from root references, mark everything reachable and free the rest; generational collectors refine this by concentrating on young objects, since most objects become unreachable soon after creation. Reference counting instead tracks how many references point to each object and frees it when the count reaches zero, although it needs extra help to reclaim cycles of objects that refer to each other. CPython combines reference counting with a cycle detector.

Garbage collection eliminates whole classes of bugs, including dangling pointers, double frees and many memory leaks, and it is a major reason languages such as Java, C#, Go, Python and JavaScript are considered safer and more productive than C. The costs are extra memory overhead and pauses or background work while the collector runs, which matter for games, audio software and low-latency systems. Rust takes a different path, enforcing memory safety at compile time through ownership rules without a garbage collector, while C and C++ leave memory management to programmers.

Key points

  • A garbage collector reclaims memory from objects that the program can no longer reach.
  • John McCarthy introduced garbage collection around 1959 as part of the Lisp language.
  • Tracing collectors and reference counting are the two main approaches, often combined in practice.
  • Collection removes many memory bugs but adds overhead and can cause pauses in latency-sensitive software.

Related entries

Glossary term

Memory Leak

Programming

Bug in which a program keeps holding memory it no longer needs, so its memory use grows over time and can degrade or crash the system.

Glossary term

Pointer

Programming

Variable that stores the memory address of another value, letting programs refer to data indirectly and build linked structures.

Person

John McCarthy

AIProgramming

American computer scientist who coined the term artificial intelligence, created the Lisp programming language and pioneered time-sharing.

Technology

Lisp

ProgrammingAI

Family of programming languages originated by John McCarthy in 1958, built on symbolic expressions and long associated with AI research.

Technology

Java

Programming

Class-based, object-oriented programming language from Sun Microsystems whose programs run on the Java Virtual Machine across many platforms.

Technology

Rust

ProgrammingFOSS

Systems programming language that guarantees memory safety without a garbage collector through its ownership model, first stable in 2015.

Technology

Go

ProgrammingDevOps

Statically typed, compiled programming language designed at Google for simplicity, fast builds and built-in concurrency, widely used for cloud software.

Glossary term

Type Safety

Programming

Degree to which a programming language prevents type errors, in which a value is used as if it were a different, incompatible kind of data.

Referenced by

Other entries that discuss or point to Garbage Collection.