Introduction to Garbage Collection
- What is garbage collection? Garbage collection is automatic
reclamation of computer memory storage.
- Purpose of presentation: to give our audience an overview
of garbage collection without going into too many technical details.
- Structure of presentation:
Reference Counting Algorithm
- In this algorithm, the garbage collector maintains a
count of the number of pointers to each object in memory. This count
is incremented or decremented as necessary when a reference to the object
is created or destroyed.
- When an object's reference count reaches zero, that object
- Reference counting is a simple algorithm, and is relatively
easy to implement correctly.
- Memory reclamation is interleaved with program execution,
and is hence "real-time". At each call to this algorithm, only a
bounded amount of work is done, halting program execution for only a brief
moment. This makes reference counting useful for applications in
which guaranteed response time is critical.
- Memory usage is very efficient - a dead object is reclaimed
immediately. No heap space is used up by dead objects.
- The counter may take up memory space large enough
to represent any number of pointers that might exist in the entire system.
- Use a smaller field to store counts,
and mark all unaccounted-for objects when the field's maximum count is
- Use another garbage collection method
(usually of a tracing type) to reclaim all marked objects and other objects
reachable by the pointers in the full field.
- If a group of objects contain a pointer cycle, their reference
count can never reach zero and therefore never be reclaimed. Remedy:
use a different method of garbage collection when memory becomes saturated
with these objects.
- This algorithm deals inefficiently with short-lived objects
such as stack variables. When they are created and destroyed quickly,
a lot of wasted reference counting takes place. Remedy: (Deferred
Reference Counting) Give special treatment to local variables by leaving
them out of reference counts. Problem: Counts will not reflect the
number of pointers to a variable anymore; therefore all the objects with
count zero will have to be scanned to check if they have references to
heap variables before they can be reclaimed.
- The process of reclamation is costly.
The reclamation process for an object involves linking the freed object
to "free lists" of reusable objects. The object also needs to be checked
to free all references. Therefore the whole reclamation process takes at
least a few tens of instructions per object, which makes the cost of
reclamation proportional to the number of objects allocated to the running
Reference counting is not used in general purpose programming languages
because of the above mentioned disadvantages. It is mostly used in
applications such as file, disk block management system and some simple
Whereas the Reference Counting Algorithm is at work every
time an object is referenced or dereferenced, Mark-Sweep
is usually run at specified time intervals.
- Step 1: Starting from the root set, we trace through
our graph of memory. Mark all objects reached.
- Step 2: Sweep through memory and reclaim all unmarked
- The Mark-Sweep algorithm doesn't create drag on every
single memory operation like Reference Counting.
- Every location in memory must be examined during the
sweep stage of this algorithm - this can be time-consuming.
- Can leave several gaps in used memory when objects are
swept out. This fragmentation of avaliable memory can cause serious
performance problems for applications which make heavy memory demands.
Although in practice, this problem usually isn't a huge problem, Mark-Sweep
garbage collection is usually considered unfit for high-performance systems
for exactly this reason.
This algorithm is essentially a variaton on the Mark-Sweep
algorithm just described.
- All live objects in memory are marked, just as in Mark-Sweep.
- Instead of sweeping the dead objects out from under the
live ones, the live objects are instead pushed to the beginning of the
memory space. The rest of memory is reclaimed for future use.
- The fragmentation problem of Mark-Sweep collection is
solved with this algorithm; avaliable memory is put in a big single chunk.
- Also note that the relative ordering of objects in memory
stays the same - that is, if object X has a higher memory address than
Y before garbage collection, it will still have a higher address afterwards.
This property is important for certain data structures like arrays.
- The big problem with Mark-Compact collection is time.
It requires even more time than Mark-Sweep collection, which can seriously
Copying Garbage Collection
Like the Mark-Sweep algorithm, Copying garbage collection does not really
collect garbage. The collector moves
all live objects into an area of memory, so the rest of the heap is
available to be used by the program since it contains garbage.
This method integrates the copying process into the data transversal, so
an object will only be visited once.
Stop and Copy
- In this method the heap space is divided into two contiguous semispaces
(fromspace and tospace).
During program execution, only one of these spaces is used.
- Memory is allocated linearly upwards in the current semispace as
demanded by the execution program. When the space is exhausted the program
is stopped and the garbage collector is executed.
- All live objects are copied from the current semispace to the other
semispace. The roles of the two semispaces are reversed each time the
garbage collector is invoked.
- Various algorithms can be used to transverse the data. One such
algorithm is Cheney breadth-first search copying.
- Form an initial queue of objects which can be immediately reached from the
- A "scan" pointer is advanced through the objects location by location.
Every time a pointer into fromspace is encountered, the object the
pointer refers to is copied to the end of the queue.
- When the "scan" reaches the end of the queue, all live objects have been
copied, so the garbage collector is terminated.
- The allocation of free objects is simple and fast.
- This method does not cause memory fragmentation, even when objects of
different sizes are copied.
- To increase copying collectors efficiency, increase the amount of memory
allocated for the heap space to reduce the number of times the collector is
Non-Copying Implicit Collector
This method is similar to the copying collector just described.
- In the copying collector, the set is an area
- In non-copying collection, the set can be any kind of set of part of
memory that formerly held live objects.
- The non-copying system adds two pointer
fields and a "color" field to each object. These fields link each part of
memory to a doubly-linked list that serves as a set. The color indicates
which set an object belongs to.
- The "moving of objects" in non-copying involves
unlinking the object from
a fromset doubly linked list, toggling its color, and linking it to
toset, which is another doubly linked list.
Advantages over copying
- The tracing cost of large objects is smaller.
- Objects without pointers will not be scanned.
- The non-copying method does not require language-level pointers
between objects to be changed. Therefore, fewer constraints
are imposed on the compiler.
- This method requires more instructions per object than copying does.
- Memory fragmentation is possible.
Incremental garbage collection
The previous garbage collection algorithms are not feasible for real-time
applications because they involve halting execution of the program while it
Instead, the garbage collector and the mutator (executing program) should be
interwoven. This allows the garbage collector to be run in small
increments, making the pauses in the executing program shorter and more
Unfortunately, while the collector is tracing the graph of reachable data
structures, the mutator may be changing the graph.
Tricolor Marking and Coherence
Tricolor marking is a method of marking which objects have been looked at
in a collection cycle, and determining which ones to recycle at the end of
- Have already been examined by the collector
- Are assumed to be in use by the mutator
- Are ready to be examined by the collector
- Are assumed to be in use by the mutator
- Have not yet been examined by the collector
- May or may not be in use by the mutator
The collector examines all data objects that are in use by starting with
the root stack and making successive waves of examining objects.
All objects pointed to by the root stack are colored gray.
Each gray object is viewed in turn and all of its child objects (objects
pointed to by it) are colored gray, and then it is colored black.
The mutator makes a change in the graph of objects by swapping the pointers
A->C and B->D. Now when the collector looks at object B, it is only
pointing to object C, which is already gray.
When the collector finishes its sweep (there are no more gray objects) any
remaining white objects should be garbage (unreachable) but D isn't in this
There are two basic approaches to coordinating the collector with
Read Barrier -
A read barrier detects when the mutator attempts to reference a white
object. The barrier then colors the white object gray and lets the mutator
reference it. This way the mutator is never allowed to reference white
objects and therefore cannot install a reference to a white object in a
Write Barrier -
On the write side, the mutator must do two things to fool the incremental
garbage collector. First it must write a pointer from a black object to a
white object, and second, it must destroy the original pointer to the white
object before the collector gets to it. Since it must do both of these
things, a write barrier would only have to prevent one of them from
succeeding to maintain coherence.
The first case is handled by a method known as incremental update.
This barrier notices when a pointer to a white object is stored in a black
object. The collector then converts the black object to gray, denoting
that it needs to be examined again by the collector.
In Snapshot-at-beginning, the collector ensures that the second
condition will never happen. It does this by saving a copy of pointers
when they are overwritten for later traversal by the collector. This means
that no path to a white object can be completely destroyed by the mutator.
Both read and write barriers are usually implemented in software by having
the compiler add instructions in the appropriate place. The overhead for
this is great, but less so for the write barriers because heap writes tend
to be less common than heap reads. For the read barriers, tens of percent
was a common estimate for the increase in overhead [Wilson, p23].
- One of the limitations of simple garbage collection algorithms is
that the system has to analyze all the data in heap. For example,
a Copying Algorithm has to copy all the live data every time it
used. This may cause significant increases in execution time.
- Studies in 1970s and 1980s found that large Lisp programs were
spending from 25 to 40 percent of their execution time for garbage
- Other studies show that most objects live for very short time
(the so-called "weak generational hypothesis"), so most
objects have to be deallocated during the next garbage collection.
- The opposing theory, the "strong generational hypothesis", which states that the
older an object is, the more likely it is to die, does not appear
to hold. Object lifetime distribution does not fall smoothly, and
if an object has survived a few collections, it is likely to live
- Implication: if we can concentrate on collection of young objects
and do not touch too often older ones, the amount of data
that has to be analyzed and copied is considerably reduced. We
can therefore make significant gains in garbage collection efficiency.
- This approach, which allows us to avoid analyzing older objects
during each collection (thus keeping the costs of collection down),
is called Generational Collection.
- How does it work?
- Generational garbage collection divides the heap into two
or more regions, called generations.
- Objects are always allocated in the youngest generation.
- The garbage collection algorithm scans the youngest generation
most frequently, and performs scanning of successive
generation more rarely.
- Most objects in youngest generation are deallocated during
the next scan. However, those objects that survive a few
scans or reach a certain age are advanced to the next
- Difficulties with Generational Collection:
- In order for Generational Collection to work, it must be
possible to collect data in younger generations without
collecting the older ones.
- This leads to some problems: if there exists a
pointer from object2 in the older generation to object1 in
the younger, object1 should be obviously considered alive.
- So, generational collection algorithms should check
whether there are any pointers from objects stored in
one generation to objects in other, and record
inter-generational pointers from older generations to younger
- Such pointers may arise in two situations:
- an object
containing a pointer is promoted to older generation.
pointer is directly stored in the memory.
- In the first case, inter-generation pointers can be easily
recorded by checking each object during its promotion.
The second case is harder - the collector needs to check
each pointer store and provide some extra bookkeeping in
case an inter-generational pointer is created. The process
of trapping pointer stores and recording them is called
- Overall, generational collection significantly
improves the performance
of collectors for most of programs. Such collectors are
in widespread use, including:
- all commercial Lisps
- Standard ML of New Jersey
- most commercial Smalltalk systems
- many other languages.
We hope you have enjoyed our talk. If you are interested in more
information about automatic memory management, we suggest the following
sources (all of which we consulted for this presentation):
- Papers of OOPS research group at University of Texas in
Last modified 03/04/99, last visited 05/09/99.
(page author - Sheetal V Kakkad)
- We especially recomend the paper "Uniprocessor Garbage Collection
Techniques" by Paul R. Wilson -
Last visited 05/01/99.
- Jones, Richard and Rafael, Lins. Garbage Collection:
Algorithms for Automatic Dynamic Memory Management. (c)1996 by John
Wiley & Sons Ltd, Baffins Lane, Chichester, West Sussex, England.
- http://suif.stanford.edu/~diwan/243/lecture12html/sld001.htm. Title: Lecture 12: Topics in Garbage Collection. Author not listed. Last accessed : 5/09/99.
Title : Understanding Garbage Collection. Author not listed. Last
modified: 7/7/99. Last accessed: 5/09/99.
Last modified 5/10/99 by Wyatt
Gaswick c/o THE REBELLION
The Rebellion (c)1999 consists of Dorene Mboya, Dmitry Krivin, Raphen Becker,
and Wyatt Gaswick.