4 minute read

I was working through the High CPU Lab Walkthrough 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
  • Large Object Heap (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 every time 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 contrived 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 walkthrough for Lab 4 pretty soon,

Laters, Tess