#936 – Visualizing Garbage Collection Generations

The garbage collection groups objects on the managed heap into generations.  This improves the performance of the garbage collector (GC), since it typically collects objects in Generation 0 (newest), only moving to older generations if necessary.

Below is an example of this.  To start with, we have an empty managed heap (no objects).  The entire heap is considered Generation 0, since we haven’t yet done a garbage collection.

936-001

We now allocate three Dog objects on the heap.  They are allocated in the first available space within Gen 0.

936-002

We now set the yourDog reference to null, so that Jack is no longer referenced.  Before garbage collection, the heap looks like this:

936-003

Assume that a GC pass runs now and collects objects in Gen 0 (the only generation available).  Memory for Jack is released, everything in Gen 0 is compacted, and whatever remains is marked as Gen 1.  Gen 0 now starts again at the free space pointer.

936-004

Assume that we run for a while and allocate a couple more Dog objects–Lassie and Rin Tin Tin.  Then assume that the reference to Lassie is removed and that the reference to Ruby is also removed.  Before collection, the heap looks as follows.  Notice that there are unreachable objects in both Gen 0 and Gen 1.

936-005

Assume that a garbage collection now runs and collects only Gen 0.  Memory for Lassie is reclaimed, Gen 0 is compacted and and its objects are promoted to Gen 1.  The existing Gen 1, however, is not collected and its objects remain in Gen 1.

936-008

Finally, let’s assume that the garbage collection runs one more time.  It begins with Gen 0, but there is nothing to collect.  Let’s assume that there are high memory demands that cause the GC to decide to also collect Gen 1.  It can now release memory for Ruby and then compacts Gen 1 and promotes its surviving objects to Gen 2.

936-007

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

4 Responses to #936 – Visualizing Garbage Collection Generations

  1. Pingback: Dew Drop – September 23, 2013 (#1,629) | Morning Dew

  2. Clay says:

    I’m not so sure about this explanation. I am pretty sure that an object only moves up a generation when the generation it’s currently in gets garbage collected. In your example, Kirby and Ruby would stay in generation 1 with Rin Tin Tin added to Generation 1. Only when the GC is run on gen 1 would Ruby be collected and the other dogs moved to gen 2.

    It wouldn’t make sense for objects to be promoted to the next generation if there wasn’t a GC pass on it first; you only want to promote objects that are referenced.

    If you need it I can look up the documentation on this to back up my claims.

    • Clay says:

      If you look here: http://msdn.microsoft.com/en-us/library/ee787088.aspx under Survival and Promotions it says:

      “Objects that are not reclaimed in a garbage collection are known as survivors, and are promoted to the next generation. Objects that survive a generation 0 garbage collection are promoted to generation 1; objects that survive a generation 1 garbage collection are promoted to generation 2; and objects that survive a generation 2 garbage collection remain in generation 2.”

      In that, only objects that survive a GC from gen 1 will go on to gen 2; not that on the next GC pass for any generation will the objects be promoted.

      • Sean says:

        You’re correct–objects are only promoted to the next generation if the current pass includes the generation that they are located in. I’ll update my post to maybe make this a bit more clear.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: