How to calculate memory usage as Task Manager does?
Ok so I am using WMI (.net/C#) to constantly collect data about a specific process that is running on the machine. I get the data through Win32_PerfFormattedData_PerfProc_Process class. That class has a lot of properties but those that we are interested in are as follows:
uint64 PageFileBytes;
Value, in bytes, that this process has used in the paging file(s). Paging files store pages of memory used by the process that are not contained in other files. Paging files are shared by all processes and lack of space in paging files can prevent other processes from allocating memory.
uint32 PoolNonpagedBytes;
Value, in bytes, in the nonpaged pool, an area of system memory (physical memory used by the operating system) for objects that cannot be written to disk, but must remain in physical memory as long as they are allocated. The PoolNonpagedBytes in Win32_PerfFormattedData_PerfOS_Memory is calculated differently than the PoolPagedBytes property in Win32_PerfFormattedData_PerfProc_Process, so it might not equal the total of PoolPagedBytes for all instances of Win32_PerfFormattedData_PerfProc_Process. This property displays the last observed value only; it is not an average.
uint32 PoolPagedBytes;
Value, in bytes, in the paged pool, an area of system memory (physical memory used by the operating system) for objects that can be written to disk when they are not being used. The PoolNonpagedBytes property in Win32_PerfFormattedData_PerfOS_Memory is calculated differently than the PoolPagedBytes property in Win32_PerfFormattedData_PerfProc_Process, so it might not equal the total of PoolPagedBytes for all instances of Win32_PerfFormattedData_PerfProc_Process. This property displays the last observed value only; it is not an average.
uint64 PrivateBytes;
Current value, in bytes, that this process has allocated that cannot be shared with other processes.
uint64 VirtualBytes;
Current size, in bytes, of the virtual address space that the process is using. Use of virtual address space does not necessarily imply corresponding use of either disk or main memory pages. Virtual space is finite and, by using too much, the process can limit its ability to load libraries.
uint64 WorkingSet;
Maximum number, in bytes, in the working set of this process at any point in time. The working set is the set of memory pages touched recently by the threads in the process. If free memory in the computer is above a threshold, pages are left in the working set of a process even if they are not in use. When free memory falls below a threshold, pages are trimmed from working sets. If they are required, they are then soft-faulted back into the working set before they leave main memory.
I am currently using the WorkingSet
field to report the process' memory usage. However that does not align with what Task Manger is showing. I tried with PrivateBytes
but that's not "correct" too. The process that the app monitors is a .NET process (if that matters at all) and it gets reported by the app to use at least 100MBs more memory than what Task Manager is showing at the same time.
So the question is what is the "formula" to calculate the best approximation of process' memory usage as shown by Task Manager?
Win32_PerfFormattedData_PerfProc_Process
is the correct class. The property it pulls from is WorkingSetPrivate
. No formula/calculation needed.
上一篇: 在虚拟地址系统上处理地址空间
下一篇: 如何计算任务管理器的内存使用情况?