Even if most of the heapspace is garbage,
a mark and sweep algorithm will touch
the entire heap. In such cases it would
be better if the algorithm only touched
the live objects.
Copying collection is such an
algorithm. The basic idea is:
The heap is divided into two
spaces, the from-space
and the to-space.
We start out by allocating
objects in the from-space.
When from-space is full,
all live objects are copied from
from-space to to-space.
We then continue allocating in to-space
until it fills up, and a new GC
starts.
An important side-effect of copying collection
is that we get automatic compaction -
after a collection to-space consists
of the live objects in a contiguous piece
of memory, followed by the free space.
This sounds really easy, but 1#1:
We have to traverse the object graph
(just like in mark and sweep), and
so we need to decide the order in
which this should be done, depth-first
or breadth-first.
DFS requires a stack (but we can, of
course, use pointer reversal just as with
mark and sweep), and BFS a queue.
We will see later that encoding a queue
is very simple, and hence most implementations
of copying collection make use of BFS.
An object in from-space will generally
have several objects pointing to it.
So, when an object is moved from from-space
to to-space we have to make sure that
we change the pointers to point to the new
copy.