none
Why Garbage collector has only 3 generations adn how GC allocate memeory to generation 0 to 2 ? RRS feed

  • Question

  • Hello All,

    I have very basic question about GC, I am learning about GC and I know about its execution algorithm of generation 0 to generation 2.

    I have below two questions about GC .

    1. How GC decide the specific amount of memory need to allocate the Generation 0, Generation 1 and Generation 2 , Is there any specific strategy that GC allocate some amount of memory to each generation ?

     

    2. Why there are only 3 generations in GC , Why not more ?

    Thanks in advance,

    Aniruddha

    Sunday, May 19, 2013 5:08 AM

Answers

  • Hi,

    When you want to "bump" your thread please post again in the same thread so that others will see something unread rather the creating a new thread (now we have two separate threads on the same subject).

    See http://msdn.microsoft.com/en-us/library/ee787088.aspx or http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

    For #1 : "When the garbage collector detects that the survival rate is high in a generation, it increases the threshold of allocations for that generation, so the next collection gets a substantial size of reclaimed memory. The CLR continually balances two priorities: not letting an application's working set get too big and not letting the garbage collection take too much time."

    For #2 : it has a strike a balance between efficiency and speed. I guess that 3 seemed enough (short, long and intermidate lifetimes) and that adding generations would perhaps not help.

    Also sometimes some context can help. Do you have a particular issue or are you curious ? (you even have books about garbage collectors).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Sunday, May 19, 2013 12:44 PM

All replies

  • Hello

    Anyone can help me here ?

    http://social.msdn.microsoft.com/Forums/en-US/clr/thread/84896082-3c21-437d-b912-21ae8704da4e

    Please have look on my questions

    Sunday, May 19, 2013 10:25 AM
  • Hi,

    When you want to "bump" your thread please post again in the same thread so that others will see something unread rather the creating a new thread (now we have two separate threads on the same subject).

    See http://msdn.microsoft.com/en-us/library/ee787088.aspx or http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

    For #1 : "When the garbage collector detects that the survival rate is high in a generation, it increases the threshold of allocations for that generation, so the next collection gets a substantial size of reclaimed memory. The CLR continually balances two priorities: not letting an application's working set get too big and not letting the garbage collection take too much time."

    For #2 : it has a strike a balance between efficiency and speed. I guess that 3 seemed enough (short, long and intermidate lifetimes) and that adding generations would perhaps not help.

    Also sometimes some context can help. Do you have a particular issue or are you curious ? (you even have books about garbage collectors).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Sunday, May 19, 2013 12:44 PM
  • Proposed answer at http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/c1051e1d-50d9-4c7f-9cac-64745e88657c


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Sunday, May 19, 2013 12:45 PM
  • Hi,

    I'll start with your second question: Why only 3 generations? - Because GC.MaxGeneration returns 2? Don't worry, that was only a joke.

    There's a logical reason to it:

    Objects in Gen0 were never inspected by the GC
    Objects in Gen1 were inspected once by the GC
    Objects in Gen2 were inspected two or more times by the GC

    Now to your first question: How much memory does the GC allocate to every generation?

    The GC works with budgets. Upon process intialization it allocates the following budgets (approximately):

    Gen0: 256 KB (to fit in the CPU's L2 cache)
    Gen1: 2 MB
    Gen2: 10 MB

    But since the GC adapts to the pattern of allocation and to the lifetime of generation objects, it can shrink or grow this budgets to boost performance.

    You can find these and more detail in Jeffrey Richter's book CLR via C#, 3rd ed., chapter 21: Automatic Memory Management (Garbage Collection). N.B. The 4th edition of Richter's book doesn't mention any figures for the budgets.

    Marcel


    Sunday, May 19, 2013 5:19 PM