#934 – How Generations Help the Garbage Collector Run More Efficiently

Objects allocated on the managed heap are grouped into generations by the garbage collector:

  • Generation 0 – Newest objects
  • Generation 1 – Objects that have survived one GC pass
  • Generation 2 – Objects that have survived more than one GC pass

When the garbage collector performs a GC pass, it looks only at objects in generation 0, releasing memory for any that are no longer reachable.  In this way, the garbage collector is more efficient, because it is only looking at a portion of the managed heap.

If the garbage collector has collected Gen 0, but the application requires more memory, the garbage collector can then garbage collect generation 1.  If the application still requires memory after collecting generation 1, it can move on to generation 2.

This scheme relies on the fact that if an object survives one GC pass, it likely has a longer lifetime and will survive future passes.

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #934 – How Generations Help the Garbage Collector Run More Efficiently

  1. Pingback: Dew Drop – September 19, 2013 (#1,627) | Morning Dew

  2. Pingback: #936 – Visualizing Garbage Collection Generations | 2,000 Things You Should Know About C#

Leave a comment