What order the GC collect generations? RRS feed

  • Question

  • I have a question about the order of generation to collect. It looks not an important question, but it will be helpful to my clear understanding about the GC.

    My Question is:
    What order the GC collect generations?
    Does it collect from Gen0 to Gen2 or from Gen2 to Gen0?

    If the GC collects generations from Gen2 to Gen0, I think it must know the triggered collection is
    ephemeral generations or not. If it does, when and how it can know which generation it should collect?
    (Of course, assume that it is not triggered by LOH, which always needs a full collection.)

    I think it is natural and easy to implement that the GC just collects Gen 0 and decide to collect an older generation or not.


    Wednesday, April 13, 2011 5:00 PM


  • The GC internals are not published -they are an implementation detail of the runtime, and change between versions.  The collections, in general, are triggered by memory pressure on the system.  As the application uses more memory and the system has more memory pressure (less available memory), the GC will run more often.

    That being said - the GC will collection Gen0 very frequently - Gen1 more rarely, and Gen2 much more rarely.  The heuristics used for determining which generation to clean is private implementation details.  However, a "Gen2" collection collections the entire GC - it's not that it only collections information in gen2, but rather that it collections everything in 0, 1, and 2 (and the LOH, which is only swept during Gen2 collections).



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by eryang Tuesday, April 19, 2011 2:21 AM
    • Marked as answer by eryang Monday, April 25, 2011 9:40 AM
    Wednesday, April 13, 2011 5:23 PM