在C#中以百分比获取内存使用率 [英] Getting Memory Usage in percentage in C#

查看:863
本文介绍了在C#中以百分比获取内存使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想以百分比(%)获取特定进程的内存使用情况。下面是我用于CPU的代码。但我无法为记忆得到同样的东西。内存性能中有如此多的计数器,我很困惑如何计算,或者我们可以直接以百分比(%)显示。请帮助。





Hi,
I want to get the Memory Usage of a particular process in percentage (%). Below is the code which I used for CPU. But I am unable to get the same for Memory. There are so many counters in Memory Performance that I''m confused how to calculate or we can directly get the display in percentage (%). Pls help.


PerformanceCounterCategory cpuProcessCategory = new PerformanceCounterCategory("Process");
               string[] instanceNames = cpuProcessCategory.GetInstanceNames();

             

               foreach (string name in instanceNames)
               {
                   try
                   {

PerformanceCounter cpuProcess = new PerformanceCounter("Process", "% Processor Time", name);
 float cpuUsage = cpuProcess.NextValue();
 Console.Write("Process: '{0}'   ", name);
}
catch
{}
}

推荐答案

嗨。

1)打开MMC

2)文件 - >添加/删除管理单元

3)在可用管理单元列表中,找到性能,标记并单击添加

4)展开树视图并选择性能然后您将看到性能图。

5)右键单击grapch并选择Add counters



然后打开一个窗口在那里你可以看到你可以在代码中使用的所有可能的计数器。
Hi.
1) Open MMC
2) File -> Add/Remove snap-in
3) In the list of available snap-ins, locate "Performance", mark it and click add
4) Expand the treeview and select "Performance" You will then see the performance graph.
5) Rightclick the grapch and select "Add counters"

Then a windows opens and there you can see all possible counters you can use in your code.


这篇关于在C#中以百分比获取内存使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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