问题描述:

Well... is it possible to get the CPU and Memory usage in uwp? (in percentage value).

What i've done for CPU is i got the user time and kernel time but for using those two values, I need total cpu time which is not available to get.

For the memory, i used MemoryManager.GetProcessMemoryReport(); and get TotalWorkingSetUsage and PrivateWorkingSetUsage.

The problem of this is it the value reported seems too low. It should be around 40~50% but it shows only around 30%. I used

(PrivateWorkingSetUsage / TotalWorkingSetUsage) *100

got each of processes memory usage as using ProcessDiagnosticInfo.

Code looks as follows

private int getCpuPerformance()

{

IReadOnlyList<ProcessDiagnosticInfo> list = ProcessDiagnosticInfo.GetForProcesses();

double sumUsage = 0, sumTotal = 0;

foreach (var item in list)

{

sumUsage += item.CpuUsage.GetReport().UserTime.Milliseconds;

sumTotal += item.CpuUsage.GetReport().UserTime.Milliseconds + item.CpuUsage.GetReport().KernelTime.Milliseconds;

}

sumTotal = sumUsage * 100 / sumTotal;

return (int)sumTotal;

}

private int getMemoryPerformance()

{

IReadOnlyList<ProcessDiagnosticInfo> list = ProcessDiagnosticInfo.GetForProcesses();

var a = MemoryManager.GetProcessMemoryReport();

long b = (long)a.TotalWorkingSetUsage;

long c = (long)a.PrivateWorkingSetUsage;

return (int)(c * 100 / b);

}

Please help me to get cpu and memory usage in UWP programming

P.S. uwp does not support the PerformanceCounter API which is useful in plain c# unfortunately

相关阅读:
Top