Automatic garbage collection
In computing, garbage collection is a system of automatic memory management which seeks to reclaim memory used by objects which will never be referenced in the future. It is commonly abbreviated as GC. The part of a system which performs garbage collection is called a garbage collector.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. Note that some language semantics imply garbage collection without explicit mention: for example, the lambda calculus is a language with a specification that implies garbage collection.
The basic principle of how a garbage collector works is:
- Determine what data objects in a program cannot be referenced in the future
- Reclaim the storage used by those objects
While garbage collection assists the management of memory, the feature is also almost always necessary in order to make a programming language type safe.
| Table of contents |
|
2 Reference counting 3 Languages whose standard implementations use automatic garbage collection 4 External links |
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.
Some variations on the algorithm do not preserve the tricolour invariant but they use a modified form for which all the important properties hold.
Statistically speaking, the objects most recently created in the runtime system are also those most likely to quickly become unreachable. A Generational GC divides objects into generations and will generally only place the objects of a subset of all the generations into the initial white set (the grey set being everything else). This can result in faster cycles.
The primary problems with tracing garbage collection arise from the nature of how it is invoked. A collection cycle can occur at any time and can require an arbitrarily long amount of time to complete. While acceptable in many applications, in others for which timing and quick response is critical such as real-time applications, it may cause disaster. Incremental garbage collection helps deal with this problem.
The other main problem is that a large amount of garbage can build up very quickly, particularly in functional programming languages, before the garbage collector has a chance to collect any of it, resulting in allocation inefficiencies, a temporary bloating in the image size, and a long collection cycle once it occurs; we say that it is not prompt.
To see how tracing garbage collectors are not prompt, consider this example:
With a naive tracing garbage collector, each time through the loop more and more memory is allocated, until all the memory in the system is consumed and the garbage collector is forcibly invoked. It's clear why this is unacceptable when we realize that this code never needs more than the memory required for one object!
Finally, a fault shared by all existing garbage collectors is the conservative assumption that memory is still in use if it is still reachable. In many systems, references are kept long after they cease to be used. A popular research area is to find less conservative ways of collecting objects that will never be used again; that is, to safely collect objects to which references still exist. In systems with standard garbage-collection, freeing of this memory can be accelerated by explicitly destroying pointers, but this is akin to explicitly freeing memory, defeating the purpose of garbage collection.
As one simple example, consider the command-line arguments passed to the startup function. Typically, the program which uses them will parse these arguments and then perform a command accordingly, never touching the arguments themselves again. But they are still kept in memory, because references to them still exist at the bottom of the call stack, in the entry function. Although these objects occupy minimal memory, similar situations often occur with very large or numerous objects.
All of these are often used as arguments against garbage collection and for explicit memory handling, particularly in time-critical applications. However, other forms of garbage collection do not necessarily share all of these faults, although the last is nearly universal, but they may have different faults of their own; all options should be considered when deciding whether to use garbage collection.
Tracing garbage collectors
Reachability of an object
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.Basic algorithm
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.Variants of the basic algorithm
The basic algorithm has several variants.
Mark and sweep
Tracing collectors can also be divided by considering how the three sets (of white, grey, and black objects) are implemented. A Mark sweep GC maintains a bit (or two) with each object to record whether it is white or black; the grey set is either maintained as a separate list or using another bit. A Copying GC identifies grey and black objects by copying them to another area of memory (the copy space) and often distinguishes black from grey objects by dividing the copy space into two portions (in the simple case by maintaining a single pointer that marks the division between black and grey objects).Generational GC
There is the issue of when to perform a cycle and what objects to place in the initial grey set. A simple collector will always put only the roots in the initial grey set, and everything else will be initially white. Disadvantages of Tracing Garbage Collectors
a := 1
repeat forever
a := a * 2
repeat a times
x := new object
x := 0
Reference counting
Reference counting is a form of garbage collection where each object stores a count of the number of references to it, and the object is reclaimed when the count reaches zero. For data structures which do not involve cycles of reference, such as pointers to strings, it is preferable to other forms of garbage collection. Even for data structures in which cycles of reference may be present, it's typically able to recover memory more quickly and in a more timely fashion, despite requiring additional mechanisms to deal with the cycles. See reference counting for more information.Languages whose standard implementations use automatic garbage collection
External links