SamWM
SamWM

Reputation: 5356

What is the .NET equivalent of PHP's memory_get_usage?

PHP has the function memory_get_usage to report the amount of memory a PHP script has. How can you do the same thing in .NET (ASP.NET C#)?

Also, can you report on how much memory an object is taking (e.g. SiteMap or DataTable)?

Upvotes: 3

Views: 141

Answers (1)

Mikael Svenson
Mikael Svenson

Reputation: 39695

For your current process you can use

Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;
long privateMemory = currentProcess.PrivateMemorySize64;

and

long managedMemory = GC.GetTotalMemory(true);

which will report the amount of managed memory allocated.

Getting the size for value types can be done with

var size = sizeof(int);

For an arbitrary object it's a bit more tricky since it can consist of many smaller objects of unknow size.

Also see

Upvotes: 2

Related Questions