简体   繁体   English

我正在使用PerformanceCounter获取进程内存使用情况。 如何以与任务管理器相同的单位显示内存使用情况?

[英]I'm getting a process memory usage using PerformanceCounter. How can i display the memory usage in the same units as in task manager?

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
            }
            else
            {
                while (true)
                {
                    Process[] processes = Process.GetProcessesByName("GameCapture");

                    PerformanceCounter performanceCounter = new PerformanceCounter();
                    performanceCounter.CategoryName = "Process";
                    performanceCounter.CounterName = "Working Set";
                    performanceCounter.InstanceName = processes[0].ProcessName;
                    worker.ReportProgress(0, ((uint)performanceCounter.NextValue() / 1024).ToString("N0"));
                }
            }
        }

What i get is in my program 530.7 MB In task manager same process i see it around 445.3 MB 我得到的是在我的程序中530.7 MB在任务管理器中,相同的过程我看到它约为445.3 MB

Why is the big difference between my program and the task manager ? 为什么我的程序和任务管理器之间有很大的区别? what should i do if i wanted to display in my program the task manager value ? 如果我想在程序中显示任务管理器值,该怎么办?

Working set represents the size of all pages belonging to the process. 工作集表示属于该进程的所有页面的大小。 This variable shrinks and grows when pages are moved to the page file and when they are called back into main memory, respectively. 当页面分别移至页面文件和将其调回主存储器时,此变量会缩小和增大。 It doesn't refer exclusively to memory your application uses, as such some shared memory might be counted twice in this metric. 它并不专门指应用程序使用的内存,因为某些共享内存可能在此指标中被计算两次。 Look here for more info. 在这里查看更多信息。

Working set - Private is probably the metric you are looking for. 工作集-私有可能是您正在寻找的指标。 Windows Task manager uses working set private as its memory usage metric. Windows任务管理器使用专用工作集作为其内存使用率指标。 It doesn't concern itself with the page file, so you get an accurate representation of the impact on your physical ram, and it doesn't count shared objects twice. 它与页面文件无关,因此您可以准确表示对物理内存的影响,并且不会对共享对象进行两次计数。

PerformanceCounter performanceCounter = new PerformanceCounter();

performanceCounter.CategoryName = "Process";
performanceCounter.CounterName = "Working Set - Private";
performanceCounter.InstanceName = processes[0].ProcessName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM