Paul
Paul

Reputation: 463

Anomalies with using process.WorkingSet64 to measure memory usage

I've found many references here on SO on the use of this method to retrieve the current memory usage of, in my case, a 32-bit process running on 64-bit Windows 7. My code is

System.Diagnostics.Process[] processes =
    System.Diagnostics.Process.GetProcessesByName("ProcessName");
if (processes.Count() != 1)
{
    throw exception here, only one instance allowed;
}

System.Diagnostics.Process process = processes[0];
memoryUsed = process.WorkingSet64 / (1024f * 1024f);

My application dynamically allocates and frees heap memory, which I can confirm with the standard Windows Task Manager. However, every time I invoke the code shown above, the retrieved memory used is always higher than be previous count. Also, it doesn't agree with the TaskManager. Acording to the Task Manager it is about 249 MB, versus 280 MB retrieved programatically.

Am I doing something silly?

Upvotes: 2

Views: 6360

Answers (2)

Dustin Kingen
Dustin Kingen

Reputation: 21245

See:

Process.WorkingSet64

Remarks:

The value returned by this property represents the current size of working set memory used by the process.

The default memory column shown in Windows Task Manager Processes tab is Memory (Private Working Set)

What do the Task Manager memory columns mean?

Memory - Private Working Set

Subset of working set that specifically describes the amount of memory a process is using that cannot be shared by other processes.

In other words if you want the full working set you may want to use Resource Monitor.

perfmon.exe /res

Upvotes: 4

alex
alex

Reputation: 12654

There are multiple process counters related to memory - working set, private working set, shared working set, commit size, e.t.c. Maybe task manager is showing different counter from what you are retrieving using WorkingSet64.

Try adding to task manager all memory-related columns to see which matches your reading closer.

Upvotes: 1

Related Questions