C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: You can see that memory returns to its low level after the garbage collection.
C# program that uses GC.Collect
using System;
class Program
{
static void Main()
{
long mem1 = GC.GetTotalMemory(false);
{
// Allocate an array and make it unreachable.
int[] values = new int[50000];
values = null;
}
long mem2 = GC.GetTotalMemory(false);
{
// Collect garbage.
GC.Collect();
}
long mem3 = GC.GetTotalMemory(false);
{
Console.WriteLine(mem1);
Console.WriteLine(mem2);
Console.WriteLine(mem3);
}
}
}
Output
45664
245696
33244
Tip: With CollectionCount and the GC.MaxGeneration property, we get this information.
Note: Because most objects die young, generations are used to optimize which elements are scanned.
And: Newer objects are scanned more often because they are most likely to have become dead.
Result: On my computer, the program resulted in 15 generation 0 garbage collections.
C# program that uses CollectionCount
using System;
class Program
{
static void Main()
{
// This loop does a lot of allocations!
for (int i = 0; i < 100; i++)
{
for (int a = 0; a < 1000; a++)
{
System.IO.Path.GetRandomFileName();
System.IO.Path.GetRandomFileName();
}
System.Threading.Thread.Sleep(1);
}
// Display collection counts.
for (int i = 0; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i);
Console.WriteLine(count);
}
}
}
Output
15
0
0
Parameter: The method receives one parameter of type bool, which lets you demand a garbage collection to occur before taking the numbers.
BoolNext: This program shows the memory difference after allocating ten million bytes and then collecting them.
Info: We pass a bool to GC.GetTotalMemory. False indicates that an expensive collection should not be forced.
FalseUsually: Forcing a GC has no benefit in programs. It just adds to the complexity. It is best to leave this parameter as false.
C# program that uses GC
using System;
class Program
{
static void Main()
{
long bytes1 = GC.GetTotalMemory(false); // Get memory in bytes
byte[] memory = new byte[1000 * 1000 * 10]; // Ten million bytes
memory[0] = 1; // Set memory (prevent allocation from being optimized out)
long bytes2 = GC.GetTotalMemory(false); // Get memory
long bytes3 = GC.GetTotalMemory(true); // Get memory
Console.WriteLine(bytes1);
Console.WriteLine(bytes2);
Console.WriteLine(bytes2 - bytes1); // Write difference
Console.WriteLine(bytes3);
Console.WriteLine(bytes3 - bytes2); // Write difference
Console.ReadLine();
}
}
Output
21060 Program started with these bytes.
10021092 After ten million bytes allocated.
10000032 Difference.
12860 After garbage collection.
-10008232 Difference.
And: Programs that allocate data on the managed heap are called mutators. One managed heap can have many mutators.
Note: When you invoke GC.GetTotalMemory, you will be counting the allocations from all the mutator programs.
Info: In ASP.NET, GC.Collect on one application will cause a performance hit on all web sites in the same pool.
Warning: This method adds more complexity to your program and probably even reduces overall performance.
And: This item can store the previous figure in a static field, and then subtract the second from the first to see the change.