If broken it is, fix it you should

Using the powers of the debugger to solve the problems of the world - and a bag of chips    by Tess Ferrandez, ASP.NET Escalation Engineer (Microsoft)

Generational GC (Garbage Collector) - A post-it analogy

Generational GC (Garbage Collector) - A post-it analogy

  • Comments 7

I was working through the High CPU Lab Review which is basically caused by high CPU in GC. To understand what happens you need to know what a generational GC is and why it's useful.

Last year sometime I used a restaurant as an analogy for how memory allocations in general and managed allocations in particular work and it turned out to be a pretty popular post. I guess analogies just make things stick in a way that pure technical descriptions don't:)

Well, I had a discussion a long time ago with the guy that came up with the restaurant analogy about generational GCs (like the .NET GC) and sometimes the discussions just space out way too much but here is the gist of my simplified explanation of a generational GC. 

 

Post-It Analogy

Rather than using Gen 0, Gen 1, Gen 2 and LOH (Large object heap) it uses a typical note taking system.

Gen 0 = Post-It-Notes
Gen 1 = Binder
Gen 2 = File Cabinet
LOH = Book Shelf

As people come by my desk or call me I take notes on post-it stickers and stick them on the frame of my screen to remember things like "look at case x", "check the progress of y", "call z", "set up a mtg with w" etc.

After a while, the frame of my screen gets pretty cluttered (I have reached the limit for how many post-its I can put on my screen) so I decide to do some garbage collection.  I look through the notes and throw away all the post-it notes that are no longer relevant.  I take the ones that are still relevant when I've done the garbage collection like "x's phone number", and put them in a binder (Gen 1), so after my garbage collection my post-it note collection (Gen 0) is empty. 

I continue like this for while, doing regular post-it cleanups, until the binder is starting to get full (i.e. I reach the Gen 1 limit).  When the binder starts to fill up I do a Gen 1/binder garbage collection and clean out any notes that are out of date.  The ones that are still relevant I file away in a file cabinet for long-term storage.  While I am cleaning out my binder I take the opportunity to clear out the sticky notes on my screen as well (Gen 0).

Once in a while the file cabinet gets so full that I would have to go and buy a new one if I didn't clean it out.  I really don't want to do that all that often since I have pretty limited space in my office so when that happens I clean out any notes that I no longer need from the cabinet, and the binder and the sticky-notes collection.  The key here is that cleaning out the sticky-notes is pretty quick, preferably I would like to get rid of as much as possible in this step since I really hate to have to go through the binder and the cabinets and clean those out. That's pretty much a waste of a sunday afternoon.  

By the way, I never mentioned the Book shelf (Large Object Heap).  If I get something that won't fit on a sticky note, like a book, an article or something like that that would be pretty cumbersome to stick on the frame of the screen, I'll stick it in the book shelf.  Naturally I don't really want to have to clean up the book shelf all the time cause it takes a while to look through all the books and rearrange them all the time so I pretty much leave those there until I need more book shelf space.  Anytime I do need a new book shelf though I figure that I might as well do a full notes cleanup as well to get rid of as much garbage as possible.

So anyways, it would be really bad if I would have to go through both the book shelf, the cabinet, the binder and my sticky-notes collection everytime I would clean up.  That's exactly what will happen if all my notes are large and end up on the book shelf.  In other words, if all the objects I allocate end up on the large object heap and that's exactly what happens in Lab 4.

 

Taking it a bit too far

After getting to this point in the analogy, me and my colleague spaced out quite a bit and came up with analogies for cards and bricks and other stuff relating to the GC but I think that was taking it a little too far:)  There is a pretty nice PPT here if you want to delve deeper into how the GC works.

I know this analogy is a bit contrieved and as with all analogies not everything can be mapped from one realm to another:)  but I like analogies cause they make me think about things in a different way.  Believe me, I've come up with worse analogies than this one (like a supermarket analogy to explain why you shouldn't store STA objects in ASP Session state:) go figure:))

 

I'll be posting the review for Lab 4 pretty soon,

Laters,
Tess

  • Just have to add this link to a recent Channel 9 interview with Patrick Dussud about the GC http://channel9.msdn.com/ShowPost.aspx?PostID=381293

  • Link to PPT is corrupted - remove a period at the end or url.

  • I personally thought it is better than the restaurant analogy because we all at one point or another does post-it note :-))

  • Ok so if there is One or more App domain per instance of the CLR, does that mean that there is one Managed heap?

    Or is there one per App Domain?

    Loved the post-it analogy  by the way :D

  • The GC and its heaps are common to the whole process so they are common to all appdomains in a given process.

    There may be multiple heaps though, if you run the server GC there will be one regular .net GC heap per logical processor and one large object heap per processor.  Each heap can have one or more heap segments.

    You will run the server gc on multiproc boxes in processes that host the CLR such as asp.net for example.  On single proc boxes or in services that don't host the CLR (winforms apps, windows services etc.) you will run the workstation GC and in that case there is only one regular and one large object heap.

    You can also choose to run the server gc in winforms apps and windows services by setting <gcServer enabled="true" /> in the config file.  Check out the GC popquiz if i want to know more about this

    http://blogs.msdn.com/tess/archive/2007/04/10/net-garbage-collector-popquiz-followup.aspx

    Glad you guys liked the post-it analogy btw:)

  • During our ASP.NET debugging chat there were many questions around the GC and the different generations.

  • I get several emails every week through the blog asking for help on various issues.&#160; Unfortunately

Page 1 of 1 (7 items)
Leave a Comment
  • Please add 1 and 6 and type the answer here:
  • Post