windows 获取进程私有工作集内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3757379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:17:20  来源:igfitidea点击:

Get a processes Private Working Set memory

c#.netwindows

提问by timothyclifford

Trying to programmatically get the private working set of a process.

尝试以编程方式获取进程的私有工作集。

Currently I am able to get the working set without issue but having trouble getting the private working set.

目前,我能够毫无问题地获得工作集,但无法获得私有工作集。

Here's method:

这里的方法:

private void GetProcessesForServer(string serverName)
{
    var runningProcesses = new Process[0];

    try
    {
        runningProcesses = Process.GetProcesses(serverName);

    }
    catch (Exception e)
    {
        ResultsPanel.Controls.Add(new Label { Text = string.Format("There was an error: {0}", e.GetBaseException().Message) });
    }

    IOrderedEnumerable<CustomProcess> processes = runningProcesses
        .Select(process => new CustomProcess(process.Id, process.ProcessName, (process.WorkingSet64 / 1024)))
        .ToList()
        .OrderBy(process => process.ProcessName);

    if (processes.Count() > 0)
        ResultsLabel.Text = string.Format("Current running processes on {0}", ServerNamesDropDown.SelectedItem.Text);

    ResultsGridView.DataSource = processes;
    ResultsGridView.DataBind();
}

So I'm passing in a server name then trying to get all the running processes for that server then binding the list of processes to a grid view. Everything works without any issues however I need to get the private working set - similar to what you see in Windows Task manager - rather than the total working set.

所以我传入一个服务器名称,然后尝试获取该服务器的所有正在运行的进程,然后将进程列表绑定到网格视图。一切正常,没有任何问题,但是我需要获得私有工作集 - 类似于您在 Windows 任务管理器中看到的 - 而不是总工作集。

Many thanks, Tim

非常感谢,蒂姆

采纳答案by Christian.K

On Windows Vista and beyond there is the "Working Set - Private" performance counter in the "Process" category (see msdn).

在 Windows Vista 及更高版本上,“进程”类别中有“工作集 - 专用”性能计数器(请参阅msdn)。

Given you are on such a platform you could use the System.Diagonstics.PerformanceCounterclass to query this information.

鉴于您在这样的平台上,您可以使用System.Diagonstics.PerformanceCounter该类来查询此信息。

To establish a link between a process ID and a given performance counter instance, use the "ID Process" counter of a category. In other words: lookup the instance where the "ID Process" counter is your desired process ID, the read the value of the "Working Set - Private" counter.

要在进程 ID 和给定性能计数器实例之间建立链接,请使用类别的“ID Process”计数器。换句话说:查找“ID Process”计数器是您想要的进程 ID 的实例,读取“Working Set - Private”计数器的值。

Hint: if you need to query all values for all processes use the System.Diagonstics.PerformanceCounterCategory.ReadCategory()call instead, as it is much faster the reading individual counters for all processes/instances.

提示:如果您需要查询所有进程的所有值,System.Diagonstics.PerformanceCounterCategory.ReadCategory()请改用调用,因为读取所有进程/实例的单个计数器要快得多。

Update:There is an article on codeprojectthat shows how to calculate that value on XP/2000, if you must. I have not tested it, so don't blame me ;-)

更新:有一篇关于codeproject的文章展示了如何在 XP/2000 上计算该值,如果必须的话。我没有测试过,所以不要怪我;-)

Update 2: You may also want to checkout this stackoverflow question/answer.

更新 2:您可能还想查看此stackoverflow 问题/答案