C# .NET Get Memory + CPU info

Date: 2022-02-21

https://stackoverflow.com/a/278088

var me = Process.GetCurrentProcess();
Console.WriteLine("Working set {0} bytes", me.WorkingSet64);
Console.WriteLine("Total CPU time {0} sec", me.TotalProcessorTime.TotalSeconds);

foreach (var aProc in Process.GetProcesses())
    Console.WriteLine("Proc {0,30}  CPU {1,-20:n} msec", aProc.ProcessName, cpu.TotalMilliseconds);
            
foreach (var aProc in Process.GetProcesses())
    totalsize += aProc.WorkingSet64/1024.0;
    
// Via garbage collector:
var gcMemoryInfo = GC.GetGCMemoryInfo();
gcMemoryInfo.TotalAvailableMemoryBytes;

mi.TotalAvailableMemoryBytes / 1024 /1024
32500 // MB
mi.TotalCommittedBytes / 1024 /1024
29 // MB
mi.MemoryLoadBytes / 1024 /1024
18525 // MB
mi.HeapSizeBytes / 1024 /1024
13 // MB

    
/////////////////////////////    
// PerformanceCounters
/////////////////////////////

// Initialize like this:

PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;

cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
// Consume like this:
public string getCurrentCpuUsage(){
    return cpuCounter.NextValue()+"%";
}

public string getAvailableRAM(){
    return ramCounter.NextValue()+"MB";
} 
60310cookie-checkC# .NET Get Memory + CPU info