15.2 Optimization Techniques

Although this may sound
like an inefficient process compared to simply
managing memory manually, the GC incorporates various optimization
techniques to reduce the time an application is frozen while waiting
for the GC to complete (known as pause time).

The most important of these optimizations is one that makes the GC
generational. This technique takes advantage of the fact that while
many objects tend to be allocated and discarded rapidly, certain
objects are long-lived and thus don't need to be
traced during every collection.

Basically, the GC divides the managed heap into three generations.
Objects that have just been allocated are considered to be in Gen0,
objects that have survived one collection cycle are considered to be
in Gen1, and all other objects are considered to be in Gen2.

When it performs a collection, the GC initially collects only Gen0
objects. If not enough memory is reclaimed to fulfill the request,
both Gen0 and Gen1 objects are collected; if that fails as well, a
full collection of the Gen0, Gen1, and Gen2 objects is attempted.

Many other optimizations are also used to enhance the performance of
automatic memory management, and in general a GC-based application
can be expected to approach the performance of an application that
uses manual memory management.