Question

First, a little background. I am trying to write an automation test that looks for memory leaks in our product by periodically checking RAM usage. In trying to find the best way to go about this, I have settled on using PrivateMemorySize64 and GC.GetTotalMemory, like so:

long memory;
long temp;
Process[] targetProcess = Process.GetProcessesByName(Path.GetFileNameWithoutExtension("(ProcessName).exe"));                
memory = GC.GetTotalMemory(false);
temp = targetProcess[0].PrivateMemorySize64;
int b = 4;

I would have thought that PrivateMemorySize64 would return only the memory used by the process, and therefore be a consistently smaller and more accurate representation of what the named process uses. However, I'm seeing that it is consistently larger by at least one order of magnitude. That makes me wonder about its accuracy. Does anyone know why that would be, or have a recommendation of a better way to go about identifying the memory usage of the program?

Thanks

Was it helpful?

Solution

GC.GetTotalMemory retrieves the amount of memory thought to be allocated. It only knows about memory allocated by the managed components, unless you call GC.AddMemoryPressure to tell it about other memory allocated.

This property "PrivateMemorySize64" can be used to monitor memory usage on computers with 32-bit processors or 64-bit processors. The property value is equivalent to the Private Bytes performance counter for the process. Private Bytes refer to the amount of memory that the process executable has asked for, so it also includes the memory allocated by the native code.

Thus explains why PrivateMemorySize64 is bigger than GC.GetTotalMemory().

Please refer these articles:

What is private bytes, virtual bytes, working set?

C# - GC.GetTotalMemory()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top