How to calculate private working set (memory)?

前端 未结 2 970
刺人心
刺人心 2020-11-30 10:24

How do I calculate the private working set of memory using C#? I\'m interested in producing roughly the same figures as taskmgr.exe.

I\'m using the

2条回答
  •  臣服心动
    2020-11-30 11:07

    This is a highly variable number, you cannot calculate it. The Windows memory manager constantly swaps pages in and out of RAM. TaskMgr.exe gets it from a performance counter. You can get the same number like this:

    using System;
    using System.Diagnostics;
    
    class Program {
        static void Main(string[] args) {
            string prcName = Process.GetCurrentProcess().ProcessName;
            var counter = new PerformanceCounter("Process", "Working Set - Private", prcName);
            Console.WriteLine("{0}K", counter.RawValue / 1024);
            Console.ReadLine();
        }
    }
    

    Do beware that the number really doesn't mean much, it will drop when other processes get started and compete for RAM.

提交回复
热议问题