如何计算私有工作集(内存)? [英] How to calculate private working set (memory)?
问题描述
如何使用 C# 计算内存的私有工作集?我有兴趣生成与 taskmgr.exe
大致相同的数字.
How do I calculate the private working set of memory using C#? I'm interested in producing roughly the same figures as taskmgr.exe
.
我正在使用 Process
命名空间并使用诸如 WorkingSet64
和 PrivateMemorySize64
之类的方法/数据,但这些数字相差 100MB 或更多有时.
I'm using the Process
namespace and using methods/data like WorkingSet64
and PrivateMemorySize64
, but these figures are off by 100MB or more at times.
推荐答案
这是一个高度可变的数字,您无法计算它.Windows 内存管理器不断地在 RAM 中进出页面.TaskMgr.exe 从性能计数器获取它.您可以像这样获得相同的数字:
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();
}
}
请注意,这个数字实际上并没有多大意义,当其他进程启动并争夺 RAM 时,它会下降.
Do beware that the number really doesn't mean much, it will drop when other processes get started and compete for RAM.
这篇关于如何计算私有工作集(内存)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!