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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Richard Blewett (richard_at_dotnetconsult.co.uk)
Date: 10/31/04


Date: Sun, 31 Oct 2004 10:50:29 -0000


<inline>

"Namratha Shah (Nasha)" <namratha1@gmail.com> wrote in message
news:OrWmf5yvEHA.3808@TK2MSFTNGP15.phx.gbl...
> 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.
>

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.

> 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:
>

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.

> 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.

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.

> 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

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



Relevant Pages

  • Re: Wonder why GC has only 2 generations ... here is the answer.
    ... > native allocation, and can help your application improve performance. ... > The GC maintains a list of roots, which point into the GC heap. ... of moving large areas of memory outweighs the amount of memory that would be ...
    (microsoft.public.dotnet.academic)
  • Re: Wonder why GC has only 2 generations ... here is the answer.
    ... > native allocation, and can help your application improve performance. ... > The GC maintains a list of roots, which point into the GC heap. ... of moving large areas of memory outweighs the amount of memory that would be ...
    (microsoft.public.dotnet.faqs)
  • Re: Wonder why GC has only 2 generations ... here is the answer.
    ... > native allocation, and can help your application improve performance. ... > The GC maintains a list of roots, which point into the GC heap. ... of moving large areas of memory outweighs the amount of memory that would be ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: Whats the difference between the heap and the freestore?
    ... >> In general, when discussing dynamically allocated memory, I hear people ... As of a couple of days ago, I though the heap ... > When you use the term heap in the context of memory allocation it annoys ... > without saying that they have to be allocated on a stack or anything else. ...
    (comp.lang.cpp)
  • Memory problems - WinDbg and SOS: Who recognizes this pattern?
    ... Last night I managed to get a memory dump using ADPlus and I analyzed it ... ephemeral segment allocation context: none ... Large object heap starts at 0x0a0d0030 ...
    (microsoft.public.dotnet.framework.performance)