gpt4 book ai didi

c# - Process.PrivateMemorySize64 返回提交的内存而不是私有(private)的

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:17 25 4
gpt4 key购买 nike

我正在使用 .NET 4.5 在 C# 中编写一个程序,这将允许我监视特定进程的内存、CPU 和网络使用情况,然后根据我的需要绘制数据图表。

为了获得特定进程的内存使用情况,我正在检查 PrivateMemorySize64 Process 对象的属性。我希望看到该进程使用的专用内存,但它显示的是提交中的数量,正如 Windows 资源监视器所确认的那样。

我的问题是:

1) 有人知道为什么会出现这个错误吗?2)有没有解决办法?3) 如果没有修复,是否有另一种直接的方法可以获得为进程保留的私有(private)内存?

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

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

最佳答案

根据您的评论,您说您正在寻找私有(private)工作集。从此链接出现 How to calculate private working set (memory)?它确实不是 Process 类的一部分。您必须改为使用性能计数器。

从其他答案复制并粘贴以防万一由于某种原因它被删除。

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

关于c# - Process.PrivateMemorySize64 返回提交的内存而不是私有(private)的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15755162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com