您的位置:首页 > 其它

在WPF中获取程序的专用工作集内存 PerformanceCounter

2015-12-04 17:17 253 查看
使用 PerformanceCounter 获取程序的专用工作集内存并不难,但是就是得找一下属性,

通过

CategoryName


遍历

InsanceName


再通过它们遍历

CounterName


之后通过这三个属性得到我们想要的内存,

public class MemoryHelper
{
/// <summary>
/// 性能计数器组件类
/// </summary>
private static PerformanceCounter performanceCounter = null;

/// <summary>
/// 获取程序专有内存
/// </summary>
/// <returns>专有内存</returns>
public static int getPrivateMemory()
{
int result = 0;

if (performanceCounter==null)
{
performanceCounter = new PerformanceCounter("Process", "Working Set - Private", Process.GetCurrentProcess().ProcessName);
}

result = (int)(Convert.ToInt64(performanceCounter.NextValue()) / 1024.0);

return result;
}
}


使用时:

//获取专有工作集内存(例:119844[这里的单位是K])
int privateMemory = MemoryHelper.getPrivateMemory();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: