1

I am monitoring CPU usage in c#, but the values of total CPU usage are always exactly 15% less than what task manager is showing. Does anyone understand why this is the case?

private static PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
private static PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");

public static void LogPerformance()
{            
    string dateString = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss") + " - ";
    string cpuUsage = dateString + getCurrentCpuUsage() + Environment.NewLine;
    string ramUsage = dateString + getAvailableRAM() + Environment.NewLine;
    Console.WriteLine(cpuUsage);
    File.AppendAllText(cpuFile, cpuUsage);
    File.AppendAllText(ramFile, ramUsage);
}

private static string getCurrentCpuUsage()
{
    return cpuCounter.NextValue() + "%";
}

private static string getAvailableRAM()
{
    return ramCounter.NextValue() + "MB";
}
zx485
  • 28,498
  • 28
  • 50
  • 59
Nenad Birešev
  • 377
  • 2
  • 10
  • See [this blog post](https://blogs.technet.microsoft.com/winserverperformance/2009/08/06/interpreting-cpu-utilization-for-performance-analysis/). Try using “Processor Information” instead of "Processor" –  Jul 05 '18 at 21:22

0 Answers0