Process.PrivateMemorySize64返回已提交的内存,而不是私有的 [英] Process.PrivateMemorySize64 returning committed memory instead of private

查看:45
本文介绍了Process.PrivateMemorySize64返回已提交的内存,而不是私有的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET 4.5用C#编写程序,这将使我能够监视特定进程的内存,CPU和网络使用情况,然后根据需要对数据进行图表处理.

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.

为了获得特定进程的内存使用情况,我正在检查 Process 对象的rel ="nofollow"> PrivateMemorySize64 属性.我希望看到该进程使用的专用内存,但相反,它显示了提交"中的数量,这已得到Windows资源监视器的确认.

In order to obtain the memory usage for a particular process, I am checking the PrivateMemorySize64 property for that Process object. 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.

我的问题是:

1)有人知道为什么会发生此错误吗?2)是否有解决方法?3)如果没有解决方法,是否还有另一种直接的方法可以获取为进程保留的专用内存?

1) Does anybody know why this error is happening? 2) Is there a fix for it? 3) If no fix, is there another straightforward way I can obtain the private memory reserved for a process?

以下是我的代码的相关部分:

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";

推荐答案

在您的评论中,您说您正在寻找私有工作集.从此链接显示如何计算私有工作集(内存)?它确实不是Process类的一部分.您必须改为使用性能计数器.

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

这篇关于Process.PrivateMemorySize64返回已提交的内存,而不是私有的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆