Groups | Blog | Home
all groups > dotnet datatools > july 2004 >

dotnet datatools : Wonder why GC has only 2 generations ... here is the answer.



Namratha Shah \(Nasha\)
7/26/2004 8:05:20 PM
Hi Guys,

Wonder why GC has only 2 generations :

The CLR Garbage Collector is a generational, mark-and-compact collector. It
follows several principles that allow it to achieve excellent performance.
First, there is the notion that objects that are short-lived tend to be
smaller and are accessed often. The GC divides the allocation graph into
several sub-graphs, called generations, which allow it to spend as little
time collecting as possible. Gen 0 contains young, frequently used objects.
This also tends to be the smallest, and takes about 10 milliseconds to
collect. Since the GC can ignore the other generations during this
collection, it provides much higher performance. G1 and G2 are for larger,
older objects and are collected less frequently. When a G1 collection
occurs, G0 is also collected. A G2 collection is a full collection, and is
the only time the GC traverses the entire graph. It also makes intelligent
use of the CPU caches, which can tune the memory subsystem for the specific
processor on which it runs. This is an optimization not easily available in
native allocation, and can help your application improve performance.

What Happens When a Collection Occurs?

Let's walk through the steps a garbage collector takes during a collection.
The GC maintains a list of roots, which point into the GC heap. If an object
is live, there is a root to its location in the heap. Objects in the heap
can also point to each other. This graph of pointers is what the GC must
search through to free up space. The order of events is as follows:

1.. The managed heap keeps all of its allocation space in a contiguous
block, and when this block is smaller than the amount requested, the GC is
called.
2.. The GC follows each root and all the pointers that follow, maintaining
a list of the objects that are not reachable.
3.. Every object not reachable from any root is considered collectable,
and is marked for collection.

4.. Removing objects from the reachability graph makes most objects
collectable. However, some resources need to be handled specially. When you
define an object, you have the option of writing a Dispose() method or a
Finalize() method (or both). I'll talk about the differences between the
two, and when to use them later.
5.. The final step in a collection is the compaction phase. All the
objects that are in use are moved into a contiguous block, and all pointers
and roots are updated.
6.. By compacting the live objects and updating the start address of the
free space, the GC maintains that all free space is contiguous. If there is
enough space to allocate the object, the GC returns control to the program.
If not, it raises an OutOfmemoryException
P.S. -- Please mail me your comments for my articles. I hope this step from
my end is helpful to all of us.
Regards,

Namratha (Nasha)




Richard Blewett
10/31/2004 10:50:29 AM
<inline>

[quoted text, click to view]

IIRC Gen0 is sized to be the same size as the L2 cache ont he processor.
Gen0 and Gen1 are allocated in a single memory block, Gen2 (and the large
object heap) are allocated in multiple extra blocks.

[quoted text, click to view]

Note that what is termed a live root is very aggressive in release builds.
At the point when a GC runs, if local variables are not used within a
particular function beyond the current instruction point then then are not
included in the live roots. the runtime knows this because it JIT compiled
the method.

[quoted text, click to view]

Heap compaction normally occurs, however, the GC may not bother if the cost
of moving large areas of memory outweighs the amount of memory that would be
compacted.

[quoted text, click to view]

The benefit of heap compaction is 2-fold:
1) that memory allocation is very fast as there is no need to search a free
list or similar. The location of the next allocation is always known
(assuming the allocation doesn't trigger a GC).
2) objects of similar lifetimes become co-located in memory over time. These
objects have a habit of talking to eachother (hence their similar
lifetimes). So colocation means that the memory manager gets a very good
rate of cache hit.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

AddThis Social Bookmark Button