简体   繁体   English

Process.PrivateMemorySize64返回已提交的内存,而不是私有的

[英]Process.PrivateMemorySize64 returning committed memory instead of private

I'm writing a program in C# using .NET 4.5, that will allow me to monitor the memory, CPU and network usage of a particular process, and then chart that data according to my needs. 我正在使用.NET 4.5用C#编写一个程序,该程序将允许我监视特定进程的内存,CPU和网络使用情况,然后根据需要绘制该数据图表。

In order to obtain the memory usage for a particular process, I am checking the PrivateMemorySize64 property for that Process object. 为了获得特定进程的内存使用情况,我正在检查该Process对象的PrivateMemorySize64属性。 I am expecting to see the Private memory used by that process, but instead, it is showing the amount in Commit, as confirmed by the Windows Resource Monitor. 我期望看到该进程使用的专用内存,但相反,它显示了“提交”中的数量,这已由Windows资源监视器确认。

My questions are: 我的问题是:

1) Does anybody know why this error is happening? 1)有人知道为什么会发生此错误吗? 2) Is there a fix for it? 2)是否有解决方法? 3) If no fix, is there another straightforward way I can obtain the private memory reserved for a process? 3)如果没有修复,是否还有另一种直接的方法可以获取为进程保留的专用内存?

Here are the relevant parts of my code: 这是我的代码的相关部分:

using System;

// I add all the open Processes to an array
Process[] localAll = Process.GetProcesses();

// I then add all the processes to a combobox to select from
// There's a button that updates labels with requested info

Process[] p = Process.GetProcessesByName(comboBox1.SelectedItem.ToString());
label1.Text = p[0].PrivateMemorySize64.ToString() + " bytes";

From your comment you said you are looking for private working set. 从您的评论中,您说您正在寻找私有工作集。 It appears from this link How to calculate private working set (memory)? 从此链接中出现如何计算私有工作集(内存)? that it is indeed not a part of the Process class. 它确实不是Process类的一部分。 You must instead use a performance counter. 您必须改为使用性能计数器。

Copied and pasted from the other answer just in case for some reason it gets deleted. 从其他答案中复制并粘贴,以防万一由于某种原因将其删除。

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();
    }
}

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

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