Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.