Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 正在获取C中的内存使用率百分比(%)_Performance_Memory_Memory Management_Cpu Usage_Performancecounter - Fatal编程技术网

Performance 正在获取C中的内存使用率百分比(%)

Performance 正在获取C中的内存使用率百分比(%),performance,memory,memory-management,cpu-usage,performancecounter,Performance,Memory,Memory Management,Cpu Usage,Performancecounter,我想得到特定进程的内存使用率百分比。下面是我用于CPU的代码。但我无法得到同样的记忆。内存性能中有太多计数器,我不知道如何计算,或者我们可以直接获得百分比显示 static void Main(string[] args) { int i = 0; try { PerformanceCounterCategory cpuProcessCategory = new PerformanceCounterCategory("Process");

我想得到特定进程的内存使用率百分比。下面是我用于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();
}

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

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;
}

在按需分页虚拟内存操作系统上,空闲RAM统计数据量完全没有意义。如果操作系统找不到使用可用RAM的方法,那么您就买了太多。