CS.BANNED.GC_COLLECT

Do not explicitly call 'System.GC.Collect()' or 'System.GC.Collect(int)'.

This rule identifies explicit calls of the "System.GC.Collect" method. An error is reported for each occurrence.

Vulnerability and risk

Garbage collection could be very costly and should be avoided by all means-- except in benchmarking code.

Mitigation and prevention

The garbage collector will be executed automatically by the .NET framework whenever it is appropriate. If you want to gain more control over garbage collection, you should write your own code instead of calling "Collect()".

Vulnerable code example

Copy
  namespace Examples.Rules.GC
  {
      public class GC
      {
      void Method()
          {
              try
              {
                  //..
             }
             finally
             {
                 //..
                 System.GC.Collect(); // VIOLATION
             }
         }
     }
 }

Deleting line 14 will resolve the issue.