| • Science | • People | • Locations | • Timeline |
When a system has a garbage collector it is usually part of the language run-time system and integrated into the language. The language is said to be garbage collected. Garbage collection was invented by John McCarthy as part of the first Lisp system. When someone refers to a garbage collected language what is meant is that garbage collection is a feature specified by the language. There are also some languages (mostly formal languages) which would require a garbage collector in any practical implementation, but whose specifications do not require garbage collection, usually because they are not intended to be used as practical programming languages. The lambda calculus is an example of such a language.
The Symbolics Lisp machine was unique in having hardware support for garbage collection.
The basic principle of how a garbage collector works is:
Although in general it's impossible to know the moment an object has been used for the last time, garbage collectors use conservative estimates that allow them to identify when an object could not possibly be referenced in the future. For example, if there are no references to an object in the system, then it can never be referenced again. This is the criterion used by most modern garbage-collection systems, but see Disadvantages of Tracing Garbage Collectors below.
While garbage collection assists the management of memory, the feature is also almost always necessary in order to make a programming language type safe, because it prevents several classes of runtime errors. For example, it prevents dangling pointer errors, where a reference to a deallocated object is used.
Tracing garbage collectors are the most common type of garbage collector. They focus on locating reachable objects, which are objects that may be referenced in the future, and then discard all remaining objects.
More formally, objects can be reachable in only two ways:
Informally, a reachable object is one that the program could get to by starting at an object it can reach directly, and then following a chain of pointer references.
Tracing garbage collectors perform garbage collection cycles. A cycle is started when the collector decides (or is notified) that it needs to reclaim storage, which in particular happens when the system is low on memory. A cycle consists of the following steps:
The tricolour invariant is an important property of the objects and their colours. It is simply this:
Notice that the algorithm above preserves the tricolour invariant. The initial partition has no black objects, so the invariant trivially holds. Subsequently whenever an object is made black any white objects that it references are made grey, ensuring that the invariant remains true. When the last step of the algorithm is reached, because the tricolour invariant holds, none of the objects in the black set point to any of the objects in the white set (and there are no grey objects) so the white objects must be unreachable from the roots, and the system calls their finalisers and frees their storage.
Some variations on the algorithm do not preserve the tricolour invariant but they use a modified form for which all the important properties hold.