What is the .NET equivalent of PHP memory_get_usage?

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

Also, can you tell how much memory an object occupies (for example, SiteMap or DataTable)?

+3
source share
1 answer

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 allocated managed memory.

Sizing for value types can be done using

var size = sizeof(int);

For an arbitrary object, this is a little more complicated, since it can consist of many smaller objects of indefinite size.

Also see

+2

Source: https://habr.com/ru/post/1750453/


All Articles