Rule Definition
.Net framework use a garbage collector that release the memory at the most appropriate time in order to optimize the memory usage.
The GC.Collect() is a very expensive method which should be avoided.
Remediation
Review the Function / Procedure design
Violation Code Sample
using System;
class MyGCCollectClass
{
private const int maxGarbage = 1000;
static void Main()
{
// Put some objects in memory.
MyGCCollectClass.MakeSomeGarbage();
Console.WriteLine("Memory used before collection: {0:N0}",
GC.GetTotalMemory(false));
// Collect all generations of memory.
GC.Collect();
Console.WriteLine("Memory used after full collection: {0:N0}",
GC.GetTotalMemory(true));
}
static void MakeSomeGarbage()
{
Version vt;
// Create objects and release them to fill up memory with unused objects.
for(int i = 0; i < maxGarbage; i++) {
vt = new Version();
}
}
}
// The output from the example resembles the following:
// Memory used before collection: 79,392
// Memory used after full collection: 52,640
Reference
https://docs.microsoft.com/en-us/archive/blogs/ricom/when-to-call-gc-collect https://stackoverflow.com/questions/478167/when-is-it-acceptable-to-call-gc-collect/21961777#21961777
Related Technologies
Technical Criterion
Efficiency - Memory, Network and Disk Space Management
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.