There are primarily two main methods for measuring the memory usage of a .NET Framework application, using the GC class or using System.Diagnostics
The .NET Framework’s GC class contains many useful memory-related methods, including GetTotalMemory(), which returns the amount of memory the garbage collector believes is allocated to your app. The number not be exact right since some objects may not have been garbage collected yet. This, however does have the advantage of being able to inform you of the amount of memory a certain part of your app uses, as opposed to the entire process:
long memAvailable = GC.GetTotalMemory(false);
Console.WriteLine(“Before allocations: {0:N0}”,
memAvailable);
int allocSizeObj = 40000000;
byte[] bigArrayObj = new byte[allocSizeObj];
memAvailable= GC.GetTotalMemory(false);
Console.WriteLine(“After allocations: {0:N0}”, memAvailable);
The above code will output the following:
Before allocations: 651,064After allocations: 40,690,080