Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

enter image description here

like image 373
Mihail Shishkov Avatar asked Apr 01 '15 13:04

Mihail Shishkov


People also ask

Is Task Manager memory usage accurate?

The short answer: What the task manager displays is pretty good to know what your apps are using unless there is a hidden memory leak. But a hidden memory leak won't be shown on any of these tools. It would be like 90% memory used but no process showing with much memory usage. It's like the memory is lost somewhere.

How do you calculate memory usage?

Keeping in mind the formula, MEM%= 100-(((free+buffers+cached)*100)/TotalMemory).

How do I calculate memory usage percentage?

The -/+ buffers/cache line shows how much memory is used and free from the perspective of the applications. Generally speaking, if little swap is being used, memory usage isn't impacting performance at all. So, the memory utilization for the server would be 154/503*100= 30%.

What is normal memory percentage in Task Manager?

Using 30 - 38% of your RAM is normal. On many computers that is about average.


1 Answers

Win32_PerfFormattedData_PerfProc_Process is the correct class. The property it pulls from is WorkingSetPrivate. No formula/calculation needed.

like image 59
Jon Tirjan Avatar answered Sep 18 '22 17:09

Jon Tirjan