Garbage Collection at Food Courts

Maoni

February 8th, 2020

When I first started working on the GC, my predecessor was explaining the GC tuning to me. I told him that I thought it sounded like how I saw janitors work at food courts (I frequented food courts at the time 😀). And he concurred.

What I said was if you observe at a food court, in order to be productive, the janitor tries to collect a sizable amount of dirty dishes when they come out to collect, which means they collect more often when it’s busy to not risk running out of clean dishes, and less often when it’s not busy because there are very few dishes to be collected. This is the same way that the GC tunes. The dirty dishes are like the space occupied by dead objects – it was used (ie, dirtied) and can now be reclaimed. The clean dishes are like the cleared space GC hands out for people to use for new objects, ie, allocations. If GC does a collection and didn’t find much dead space, meaning high survival rate, it will wait till more cleared space is used, or in other words, more allocations are made before it does the next collection.

During meal times, food courts are busy so the clean dishes are consumed more quickly, and the janitor adjusts to that. This is the same as GC adjusts the amount of allocations before the next collection. It’s called the allocation budget. And this “adjusting” that GC does is part of its dynamic tuning. As the process runs, GC will modify this allocation budget according to the survival rate it observes to keep itself productive.

Now, the janitor needs a way to recognize if a dish is still in use – it would be quite rude for the janitor to take your dish away if you are still in the middle of using it (if they do that a lot I imagine they’d be fired…). If you are still eating, that’s pretty obvious. That’s the most common scenario. If say you need to leave temporarily (like if you suddenly remembered you needed to get a slice of cake before that dessert place closes…), you might put your jacket on the chair to indicate that you are still here. GC also needs to know if an object is still in use. The most common way is just by one object having a reference to another and that’s obvious to the GC. And there also exist other less common ways that require more effort to indicate to the GC that an object is in use, for example, the GC handles.

Over the years I’ve used this food court analogy to explain GC tuning to others and it seems to be well received, even by folks who don’t work in tech fields at all. Once I was at a class about communication and each person was required to give a 1 minute speech to explain their work. Many people in that class worked in finance and sales. I used the foot court analogy and years later I still got people from the class who enthusiastically told me that they understood how a garbage collector works 😄 thanks to the analogy, and still remembered it to that day.

The runtime team used to have what we called the “CLR Foundations” series where someone explains to the rest of the team the area they worked on. So when I explained the GC I used the food court analogy. Some of my coworkers told me they really enjoyed it. Of course as creative as they were, they started suggesting other things at a food court that could be used. I remember one suggested that we could use how people are seated to illustrate a compacting collector, as in, if there aren’t that many people, they could be sitting sparsely at tables. But if they are running out of tables, people can sit closely together to free up space to accommodate more.

A while ago the cafeteria at work (for the building I was in) started to implement this new “zero waste” thing which meant we no longer throw away garbage or put dirty dishes away ourselves. Instead a cafeteria employee acts as the garbage collector. So it’s like at a food court. And this garbage collector had a very aggressive collection policy. Sometime when people just finished eating and are still chatting at the table, they would already start a collection which meant each person would need to pass them their used dishes 😛 Knowing this I was very mindful with my food but one time I was still not quick enough – after I put my food for a few seconds they already started collecting. I sent the cafeteria manager an email to suggested a way to indicate “in use”. As always she’s extremely nice; and apparently already had a way to indicate this and just needed to remind some of her staff of it. She also said “I will also make sure they understand the importance of waiting before clearing a table after the initial lunch rush”. And I thought, “hey, that’s dynamic tuning right there!”.