3

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)?

SamWM
  • 5,196
  • 12
  • 56
  • 85

1 Answers1

2

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

Community
  • 1
  • 1
Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79