如何获取.Net Core中进程的CPU使用率和虚拟内存? [英] How to get CPU Usage and Virtual Memory for a process in .Net Core?
问题描述
在.NET Core中,如何获取给定进程的CPU使用率和虚拟内存?
In .NET Core, how to get the CPU usage and Virtual Memory for a given process?
Google搜索结果表明PerformanceCounter和DriverInfo类可以胜任。但是,PerformanceCounter& .NET Core中不提供DriverInfo类。
Google search result reveals that PerformanceCounter and DriverInfo class could do the job. However, PerformanceCounter & DriverInfo class are not available in .NET Core.
stackoverflow中有一个有关此问题的帖子:如何获取C#网站中当前的CPU / RAM /磁盘使用情况使用.NET CORE的应用程序?
There is a post in stackoverflow about this question: How to get the current CPU/RAM/Disk usage in a C# web application using .NET CORE?
但是它只能解决:
-当前进程的CPU使用率:
However it only addresses: -CPU usage for the current process:
var proc = Process.GetCurrentProcess();
我被赋予了进程(具有ProcessID整数格式)。如何在.NET Core中获取该特定进程的CPU使用率和虚拟内存?
I have been given process (with a ProcessID integer format). How do I get the CPU Usage and Virtual Memory for that particular process in .NET Core?
推荐答案
您可以使用<$ c System.Diagnostics.PerformanceCounter
You can use PerformnceCounter
in the System.Diagnostics.PerformanceCounter
package
例如,下一个代码将为您提供处理器使用总量百分比
for example, the next code will give you the total processor usage percent
var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
var value = cpuCounter.NextValue();
// In most cases you need to call .NextValue() twice
if (Math.Abs(value) <= 0.00)
value = cpuCounter.NextValue();
Console.WriteLine(value);
这篇关于如何获取.Net Core中进程的CPU使用率和虚拟内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!