【问题标题】:Getting Memory Usage in percentage (%) in C#在 C# 中以百分比 (%) 获取内存使用情况
【发布时间】:2013-04-20 11:20:49
【问题描述】:

我想以百分比 (%) 获取特定进程的内存使用情况。下面是我用于 CPU 的代码。但是我无法为内存获得相同的结果。内存性能中的计数器太多了,我很困惑如何计算或者我们可以直接以百分比(%)显示。

static void Main(string[] args)
{
    int i = 0;
    try
    {
        PerformanceCounterCategory cpuProcessCategory = new PerformanceCounterCategory("Process");
        string[] instanceNames = cpuProcessCategory.GetInstanceNames();
        Thread.Sleep(5000);

        foreach (string name in instanceNames)
        {
            try
            {
                PerformanceCounter cpuProcess = new PerformanceCounter("Process", "% Processor Time", name);
                PerformanceCounter memProcess = new PerformanceCounter("Memory", "Available KBytes");

                cpuProcess.NextValue();
                //Thread.Sleep(5000);
                float cpuUsage = cpuProcess.NextValue();
                float memUsage = memProcess.NextValue();
                //Console.ForegroundColor = ConsoleColor.Yellow;
                //Console.Write("Process:'{0}'   CPU Usage: {1}%   RAM Free: {2}KB", name, cpuUsage, memUsage);
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("Process: '{0}'   ", name);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write("CPU Usage: {0}%   ", cpuUsage);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("RAM Free: {0}KB", memUsage);
                Console.WriteLine("");

                i++;                        
            }

            catch
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("Cannot read CPU Usage for process: {0}", name);
            }
        }


    }
    catch
    {
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("Cannot retrieve Performance Counter statistics");
    }

    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine("Total no. of processes: " + i);
    Console.ReadLine();
}

【问题讨论】:

  • “可用 RAM 量”统计数据在按需分页虚拟内存操作系统上完全没有意义。如果操作系统无法找到使用可用 RAM 的方法,那么您购买的内存过多。

标签: performance memory memory-management cpu-usage performancecounter


【解决方案1】:

性能计数器不是个好主意。 请改用此代码:

var wmiObject = new ManagementObjectSearcher("select * from Win32_OperatingSystem");

var memoryValues = wmiObject.Get().Cast < ManagementObject > ().Select(mo = > new {
    FreePhysicalMemory = Double.Parse(mo["FreePhysicalMemory"].ToString()),
    TotalVisibleMemorySize = Double.Parse(mo["TotalVisibleMemorySize"].ToString())
}).FirstOrDefault();

if (memoryValues != null) {
    var percent = ((memoryValues.TotalVisibleMemorySize - memoryValues.FreePhysicalMemory) / memoryValues.TotalVisibleMemorySize) * 100;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2011-03-08
    • 2015-03-11
    • 2017-05-14
    • 2010-09-21
    • 1970-01-01
    • 2019-11-11
    相关资源
    最近更新 更多