Currently, the process of. NET Framework (. NET 4.0) only provides the acquisition of the memory working set of the process (through the workingset64 attribute), but does not provide the acquisition of the private working set. Note that after Windows Vista, the process memory in Windows Task Manager displays the private working set of the process by default. Therefore, this performance data is very important.
You can use workingset-Private of the process class of the performance counter to obtain the private working set of the process. Of course, the workset can also be obtained through the workingset attribute. In addition, both the process class and the performance counter have the peak workingset to obtain the peak value of the working set.
For example, you can use process. workingset64 and workingset private of the performance counter to monitor the memory working set of a program (taking the Task Manager taskmgr as an example.
VaR processname = "taskmgr ";
Using (VAR process = process. getprocessesbyname (processname) [0])
Using (VAR p1 = new performancecounter ("process", "Working Set-private", processname ))
Using (VAR P2 = new performancecounter ("process", "Working Set", processname ))
{
While (true)
{
// Note divided by the number of CPUs
Console. writeline ("{0} {1: n} kb", "Working Set (process type)", process. workingset64/1024 );
Console. writeline ("{0} {1: n} kb", "Working Set", process. workingset64/1024 );
Console. writeline ("{0} {1: n} kb", "private working set", p1.nextvalue ()/1024 );
Thread. Sleep (1000 );
}
}
Output:
Working Set (process class) 9,024.00 KB
Working Set 9,024.00 KB
Private workset 2,028.00 KB
Working Set (process class) 9,024.00 KB
Working Set 9,024.00 KB
Private workset 2,028.00 KB
......