Calculate Memory Usage of your App

Madhu.b.rokkam
Posted by Madhu.b.rokkam under C# category on | Points: 40 | Views : 4108
static void Main(string[] args)
{
long before = GC.GetTotalMemory(false);
Console.WriteLine("Before: {0:N0}", before);

int data = 100;
string name = "Dotnet Funda";

long after = GC.GetTotalMemory(false);
Console.WriteLine("After: {0:N0}", after);

Console.WriteLine("Approximate Allocations: {0:N0}", after - before);
Console.Read();
}


Result

Before: 11,01,624
After: 11,40,592
Approximate Allocations: 38,968

Comments or Responses

Posted by: Ndebata on: 4/14/2011 Level:Starter | Status: [Member] | Points: 10
Hi Madhu,

If I am right, you are only calculating the allocated memory usage, which may be different from the total memory usage by your application.

Thanks,
Debata
Posted by: Ndebata on: 4/14/2011 Level:Starter | Status: [Member] | Points: 10
All types of memory consumed by your application can be calculated by

var pros=Process.GetProcesses().Where(p=>p.ProcessName.Equals("ApplicationName")).FirstOrDefault();

if(pros!=null)
Console.WriteLine(pros.PrivateMemorySize64);


Regards,
Debata

Login to post response