
You want to see an example of using the GC.GetTotalMemory method from the System namespace in the .NET Framework and the C# programming language. This method returns the number of bytes allocated in the managed heap, but not in the specific program. Here we look at how you can use GC.GetTotalMemory.
First, the GC class in the .NET Framework is located in the System namespace, and you can either use the "using System" directive at the top of your program or specify the fully qualified name "System.GC.GetTotalMemory" to call this method. The method receives one parameter of type Boolean, which lets you specify whether you demand a garbage collection to occur before taking the numbers. This program shows the memory difference after allocating ten million bytes and then collecting them.
Program that uses GC [C#]
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 (to prevent allocation from being optimized out of program)
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.Force full collection parameter. The calls to the GC.GetTotalMemory method receive a single parameter in this program, either false or true. The null literal 'false' is the default here as it indicates that an expensive collection should not be forced. Usually, forcing a garbage collection has no benefit in programs and just adds to the complexity, so it is best to leave this parameter as 'false'.

Here we note that the GC.GetTotalMemory method operates on the concept of the managed heap, not the program that you are calling it from. Programs that allocate data on the managed heap are called 'mutators', and one managed heap can have many mutators. When you invoke GC.GetTotalMemory, you will be counting the allocations from all the mutator programs. On a web server, this would add other programs also running in ASP.NET.
Here we mention an excellent way of getting a general feel of the amount of memory your program is allocating on the managed heap. You can set up a special web page or dialog in your program that reports the statistics from GetTotalMemory. It can store the previous figure in a static field, and then subtract the second from the first to see the change. This does not give you precise and exact figures but can help you determine if memory is or will become a problem in your program.

Because the .NET Framework is actively developed by Microsoft, there are implementation changes in the garbage collector. Additionally, some programs are more efficient when they retain more objects in memory rather than allocating new ones each time. For these reasons, monitoring the GC figures is useful but only when weighed against many other benchmarks and metrics.
Garbage Collection Visualizations
We looked at the GC.GetTotalMemory method in the .NET Framework using the C# language and noted how it can indicate the number of bytes allocated on the managed heap. Additionally, we looked at the parameter to the method and also mentioned some ways you can use the GC class in your program to better determine its memory efficiency.
.NET Framework Info