我在这里找到了很多关于使用此方法来检索当前内存使用情况的引用资料,就我而言,是在 64 位 Windows 7 上运行的 32 位进程。我的代码是
System.Diagnostics.Process[] processes =
System.Diagnostics.Process.GetProcessesByName("ProcessName");
if (processes.Count() != 1)
{
throw exception here, only one instance allowed;
}
System.Diagnostics.Process process = processes[0];
memoryUsed = process.WorkingSet64 / (1024f * 1024f);
我的应用程序动态分配和释放堆内存,我可以通过标准 Windows 任务管理器确认这一点。然而,每次我调用上面显示的代码时,检索到的使用的内存总是高于之前的计数。另外,它与任务管理器不一致。根据任务管理器的显示,该大小约为 249 MB,而以编程方式检索的大小为 280 MB。
我是不是做了什么蠢事?
最佳答案
参见:
Remarks:
The value returned by this property represents the current size of working set memory used by the process.
Windows 任务管理器
进程
选项卡中显示的默认内存列是内存(私有(private)工作集)
What do the Task Manager memory columns mean?
Memory - Private Working Set
Subset of working set that specifically describes the amount of memory a process is using that cannot be shared by other processes.
换句话说,如果您想要完整的工作集,您可能需要使用资源监视器
。
perfmon.exe/res
关于c# - 使用 process.WorkingSet64 测量内存使用情况出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16402036/