'What makes C# Garbage Collection slow?

I was wondering which of these parameters have the most impact on GS's performance:

  • object count
  • object size
  • object generation

Specifically, which of these determine how long the CPU freezes all threads in order to clean up. I guess I'm hoping that the number of objects only causes significantly more work during execution, but does not extend the duration of the freeze. Like collecting trash into a bag may take more time, if there is more trash, but throwing the bag into a dumpster (or whatever) takes just as long, (almost) regardless of how many items are in it.

Is this thought naive?



Solution 1:[1]

There's no a single answer to this question but there is a general "The one rule to remember" from Maoni Stephens, the lead developer of .NET GC:

"What survives usually determines how much work GC needs to do; what doesn't survive usually determines how often a GC is triggered."

The relation between "how much work GC needs to do" and pauses that it introduce (as it seems you are mostly concerned about this aspect) is a different story and heavily depends on the specific GC implementation.

As I feel you are interested in GC internals, I strongly recommend to read the linked document and also watch my ".NET GC Internals" series.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Konrad Kokosa