WPF开发随笔收录-获取程序专有内存

分享一个C#获取程序当前所占用的内存大小的方法,实测跟任务管理器上的内存值一样

/// <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 / 1024.0);

    return result;
}

 

posted @ 2022-09-13 14:44  流浪g  阅读(167)  评论(0编辑  收藏  举报