Question

I'm trying to monitor memory usage. I wrote a sample c# code to be certain that I'm measuring correctly:

var list = new List<byte[]>();
int INCREMENT = 100; // 100MB
for(int i=0; i<10; i++){
  list.Add(new byte[INCREMENT * 1024 * 1024]);// 100 MB steps
  Thread.sleep(4000);
}

I used task manager and recorded the readings for "Private Working Set":

3'800k = 3.7M
3'900k = 3.8M
4'100k = 4M
4'300k = 4.2M
4'500k = 4.4M
5'200k = 5.07M
5'400k = 5.27M
5'600k = 5.47M
5'900k = 5.76M
6'100k = 5.96M

Does anyone know why the numbers make no sense?

Was it helpful?

Solution

Instead of looking at "Memory (Private Working Set)", look at "Commit Size". You may have to add it with "/View/Select Columns..." then check "Commit Size". For me it increased by about a GB, while working set went up by 3 MB.

OTHER TIPS

If you look at the definition for "Memory (private working set)" in Task Manager, it says "Amount of physical memory in use by the process that cannot be used by other processes". This is very different from "private bytes" which is the number of virtual memory bytes that cannot be shared by other processes.

The data you allocate in your sample may or may not be backed by physical memory at any given time. That's what is reflected by "Memory (private working set)". Since you never write any of that memory Windows has cleverly decided not to back the virtual memory with real memory pages. If you fill the array with data you'll see that the corresponding memory pages are allocated.

When I run the code posted above, I actually see the "commit size" in task manager increase. If I want to retrieve that in a sript, I need to use the wmi api. When I use a wmi query such as this:

SELECT PrivateBytes FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess=1234

it does not detect the increase. You can run this query either in powershell, python,...while the test app is running. I appreciate if someone can comment on this as well.

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