Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppressing 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%.

like image 946
Fantius Avatar asked Jun 10 '09 14:06

Fantius


People also ask

How to suppress warning c#?

Use a #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code.

How do you suppress code analysis errors?

Suppress violations using a global suppression file The global suppression file uses the SuppressMessage attribute. From the Error List, select the rules you want to suppress, and then right-click and select Suppress > in Suppression File.

How do I stop Visual Studio errors?

Suppress specific warnings for Visual C# or F# Or, select the project node and press Alt+Enter. Choose Build, and go to the Errors and warnings subsection. In the Suppress warnings or Suppress specific warnings box, specify the error codes of the warnings that you want to suppress, separated by semicolons.


2 Answers

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.

like image 81
Welbog Avatar answered Sep 21 '22 17:09

Welbog


You can stop the garbage collector from finalizing any of your objects using the static method:

GC.SuppressFinalize(*your object*)

More information here: link text

like image 35
Ken Pespisa Avatar answered Sep 19 '22 17:09

Ken Pespisa