I like to frequent on StackOverflow, and occasionally there is are those performance questions. They either focus on memory consumption, timing, or CPU utilization. Inevitably, a few reveal that they have scattered GC.Collect throughout their code in effort to keep memory consumption down.
I generally don’t like blanket statements, but “If you are using GC.Collect, you are doing something wrong” is a good things to keep in mind. I am hard pressed to think of scenarios where, in a production application, I would want to force a Garbage Collection. That’s what the CLR is for. It probably knows better than I do when garbage is a problem. There are plenty of reasons to not for a collection either, like forcing objects to promote to the next generation.
That got me thinking, where in the .NET Framework is GC.Collect used?
I searched the .NET Framework 4.0 GAC’s assemblies for calls to GC.Collect. Here is a diagram indicating my results (click for a larger view):
This really doesn’t come as a surprise to me, though there are a few odd balls out there.
In the next part, we’ll look at the top 50 NuGet packages to see which ones use GC.Collect.