dB.Employee
dB.Employee

Reputation: 163

Getting process ram usage in megabytes

I'm trying to retrieve the ram usage of my console application and for this purpose I'm using :

var ramUsage = Process.GetCurrentProcess().WorkingSet64;

However the value that is returned by WorkingSet64 is really big and I assume those are kbs tho I'm not 100% sure do I need to divide the number by 1024 to get the megabytes or I need to divide it by 1024*1024 ?

Can anyone shed some light on what exactly is the value returned by Process.WorkingSet64.

Upvotes: 2

Views: 10102

Answers (2)

Lithium
Lithium

Reputation: 393

The Process.WorkingSet64 property is returned in bytes. As per the MSDN article about the Property:

Property Value Type: System.Int64

The amount of physical memory, in bytes, allocated for the associated process.

https://msdn.microsoft.com/en-us/library/system.diagnostics.process.workingset64%28v=vs.110%29.aspx

In Windows, in order to get the value in megabytes you are correct in that you divide by 1024 * 1024

var ramAllocation = Process.GetCurrentProcess().WorkingSet64;
var allocationInMB = ramUsage / (1024 * 1024);

Side Note: Windows uses the JEDEC standard where 1 kilobyte = 1024 bytes. The IEC standard defines 1 kilobyte = 1000 bytes and 1 kibibyte = 1024 bytes. OSX after 10.6 uses ICE megabytes so 1 kilobyte = 1000 bytes. Some Linux distros, like Ubuntu after 10.10, use ICE prefixes (1 mebibyte = 1024 kibibyte and 1 megabyte = 1000 kilobytes). This is why sometimes when you purchase a HDD it seems smaller in Windows (1MB = 1024kB) than the advertised size (usually 1MB = 1000 kB).

Also, Cody Gray pointed out this is the number of allocated bytes, not the number of used bytes. OS's allocate more memory than the exact number of bytes the program uses, and generally, don't reclaim the memory immediately after it is not in use.

Upvotes: 1

mfeingold
mfeingold

Reputation: 7134

As per the documentation, the number returned by this property is in bytes.

Therefore, to convert it to kilobytes, you would divide it by 1024. To convert it to megabytes, you would divide it by 10242. And so on.

Upvotes: 2

Related Questions