Measure Memory Usage of .NET Applications


0

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

View the original article here


Like it? Share with your friends!

0

What's Your Reaction?

hate hate
0
hate
confused confused
0
confused
fail fail
0
fail
fun fun
0
fun
geeky geeky
0
geeky
love love
0
love
lol lol
0
lol
omg omg
0
omg
win win
0
win
Anand Narayanaswamy
Anand Narayanaswamy is the editor-in-chief of Learnxpress. He was a Microsoft Most Valuable Professional (MVP) for a period of 9 years. He is a ASPInsider based in Trivandrum, Kerala State, India. Anand is the author of Community Server Quickly published by Packt Publishing.

0 Comments

Your email address will not be published. Required fields are marked *