c# – Suppressing Garbage Collection in C#

c++garbage-collection

My application allocates a large amount of memory (millions of small objects totaling several gigabytes) and holds onto it for a long time.

  1. Is .NET wasting time checking through all of this data to do GC on it?
  2. How often does the Gen 2 GC occur (the one that checks all objects)?
  3. Is there any way to reduce it's frequency or temporarily suppress it from occurring?
  4. I know exactly when I am ready for a large amount of memory to be collected, is there any way to optimize for that? I am currently calling GC.Collect(); GC.WaitForPendingFinalizers(); at that time.

Update:
Perf counter "% Time in GC" is showing an average of 10.6%.

Best Answer

Unless you can confirm that the garbage collector is actively slowing the performance of your application, you should not take steps to cripple the functionality of your runtime environment.

Judging from your question, you have not confirmed that the GC is a problem. I severely doubt that it is.

Optimize only what needs to be optimized.