Java Reference
In-Depth Information
enough that the lifetime of most heap objects is less than the time between
collections, then deallocation of short-lived objects will appear to be free,
though longer-lived objects will still exact a cost.
It may seem that copying collectors are terribly wasteful. After all, at most
only half of the heap space is actually used. The reason for this apparent
ine
ciency is that any garbage collector that does compaction must have an
area to which live objects may be copied. Since in the worst case all heap
objects could be live, the target area must be as large as the heap itself. To
avoid copying objects more than once, copying collectors reserve a to space as
big as the from space . This is essentially a space-time trade-o
ff
, making such
collectors very fast at the expense of possibly wasted space.
If we have reason to believe that the time between garbage collections will
be greater than the average lifetime of most heap objects, then we can improve
our use of heap space. Assume that 50% or more of the heap will be garbage
when the collector is called. We can then divide the heap into 3 segments,
which we will call A , B ,and C . Initially, A and B will be used as the from space ,
utilizing two-thirds of the heap. When we copy live objects, we will copy them
into segment C , which will be big enough if half or more of the heap objects
are garbage. Then we treat C and A as the from space ,using B as the to space for
the next collection. If we are unlucky and more than half of the heap contains
live objects, we can still get by. Excess objects are copied onto an auxiliary data
space (perhaps the stack), then copied into A after all live objects in A have
been moved. This slows collection down, but only rarely (if our estimate of
50% garbage per collection is sound). Of course, this idea generalizes to more
than 3 segments. Thus if two-thirds of the heap were garbage (on average),
we could use 3 of 4 segments as from space and the last segment as to space .
Generational Garbage Collection The great strength of copying collectors
is that they do no work for objects that are born and die between collections.
However, not all heap objects are so short lived. In fact, some heap objects are
very long lived. For example, many programs create a dynamic data structure
at their start, and utilize that structure throughout the program. Copying
collectors handle long-lived objects poorly. They are repeatedly traced and
moved between semispaces without any real benefit.
Generational garbage collection techniques [Ung84] were developed to
better handle objects with varying lifetimes. The heap is divided into two
or more generations ,eachwithitsown to and from space .Newobjectsare
allocated in the youngest generation, which is collected most frequently. If an
object survives across one or more collections of the youngest generation, it is
“promoted” to the next-older generation, which is collected less often. Objects
that survive one or more collections of this generation are then moved to the
next older generation. This continues until very long-lived objects reach the
oldest generation, which is collected very infrequently (perhaps even never).
 
Search WWH ::




Custom Search