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

来源:互联网 发布:中国移动学院网络大学 编辑:程序博客网 时间:2024/05/21 14:53

使用 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();
0 0
原创粉丝点击