Garbage collection

Updated: 04/26/2017 by Computer Hope

In computer programming, garbage collection is a form of automatic memory management invented by John McCarthy in the 1950s, as part of his development of Lisp.

How garbage collection works

Computer programs allocate memory for data objects: they stake their claim to certain areas in memory, telling the rest of the system "this memory is in use, and no other programs can use it." When the program no longer needs this area of memory, it must be "released" before it can be used by other data objects, or system processes. In some programming languages, this must be done manually (the programmer must explicitly allocate and free up memory as part of the program itself).

Garbage collection automates this process by using an agent process (the "garbage collector") to search for allocated memory that is no longer used by the program. It identifies this "stale" memory and frees it, making it available to be used for other purposes.

Some programming languages require garbage collection as part of their specification (for instance, Java, C#, and Go). Other languages were designed for manual memory management, but have garbage collection implementations (like C and C++). Still, other languages such as Ada allow garbage collection and manual memory management to coexist in the same application.

Benefits of garbage collection

Garbage collection has the advantage of eliminating human error. Programs that manage memory manually may contain errors which access it incorrectly, causing the program to crash, or exposing the system to security vulnerabilities. However, garbage collection requires additional system resources, and the collection itself may occur at unpredictable times, making the speed and efficiency of programs less reliable.

Memory management, Programming terms