Replies: 2 comments 3 replies
-
I'd recommend "Pro .NET Memory Management" book, I don't think your abstract question can be answered with just one reply here |
Beta Was this translation helpful? Give feedback.
-
I got nerd-snipped by the question "why not more than 3 generations?" and researched this topic a number of ways Microsoft documentationThere are some articles articles on learn.microsoft.com about the garbage collector that address why there are generations and why there is a large object heap: They don't directly address why 3 and not some other number. LLMI asked Gemini Thinking "What is the optimally number of generations in a generational garbage collector?" and it did a pretty good job of discussing the trade offs. It included a "Why not more than three generations" section that says basically what I was planning on writing without having researched this topic:
Old Channel 9 videosThere is an Channel 9 video from 2009 with Patrick Dussud, the original author of the .NET GC. In it he mentions that one advantage of the GC is the objects in gen 0 tend to stay in the CPU cache, so by collecting just gen 0 and reusing the memory you get good cache locality. My analysis of this is that if your generation sizing is related to CPU cache size, it does not make sense to have more generations than there are layers in the memory hierarchy. There are 4 levels of cache relevant to GC (L1, L2, L3, main memory). But it might not make sense to size gen 0 to be the same as the caches in a CPU. The sizing of the ephemeral segment in .NET is pretty big (tens to hundreads of megabytes), which is larger than modern CPUs last level cache. The Myths and realities: the performance impact of garbage collection paper had a benchmark showing that the optimal eden generation size in terms of reducing caches misses. Their conclusion was a size that was several times the last level cache of their CPU. Putting all of the above together, having two generations (young and old) should be sufficient to get the cache locality benefits of a GC. There are two other videos on the site about the .NET GC that might be worth looking at for a historical perspective. Research papersAs a starting point I searched for the most cited papers about garbage collection in the ACM digital library and traced through some of the references forward and backwards. I have not read everything, but I had a hard time finding a paper that specifically experimented with increasing the number of generations. These are the most interesting papers in my opinion:
ConclusionHaving two or three generations captures most the benefits of both cache locality and the weak generational hypothesis. Having more generations would increase implementation complexity and overhead. |
Beta Was this translation helpful? Give feedback.
-
I know there are a few blogs talking about how .NET GC works. But I'd like to dive deep and learn why it is designed like this. I also readed a few blogs from the GC Architect - Maoni Stephens. Maoni‘s blogs describe how GC works but not about how GC was designed.
For example, why only 3 gen instead of 4, 5, 6 gens? Why there is no way to solve LOH issue? And so on.
Beta Was this translation helpful? Give feedback.
All reactions