简体   繁体   English

如何在不循环所有进程的情况下获取当前的 CPU 和 RAM 使用情况,以及 C# .NET 核心中有多少可用?

[英]How to get current CPU and RAM usage, and also how much is available in C# .NET core without looping over all processes?

C# 中是否有任何方法可以使用当前内存量和可用内存量,而无需遍历所有进程并添加每个 workingset64 值?

Well, you could do this:好吧,你可以这样做:

    public class MemoryMetrics
{
    public double Total;
    public double Used;
    public double Free;
}

    public class MemoryMetricsService : IMemoryMetricsService
{
    public IRuntimeInformationService RuntimeInformationService { get; set; }
    public MemoryMetricsService(IRuntimeInformationService runtimeInformationService)
    {
        this.RuntimeInformationService = runtimeInformationService;
    }
    public MemoryMetrics GetMetrics()
    {
        if (RuntimeInformationService.IsUnix())
        {
            return GetUnixMetrics();
        }

        return GetWindowsMetrics();
    }

    private MemoryMetrics GetWindowsMetrics()
    {
        var output = "";

        var info = new ProcessStartInfo();
        info.FileName = "wmic";
        info.Arguments = "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value";
        info.RedirectStandardOutput = true;

        using (var process = Process.Start(info))
        {
            output = process.StandardOutput.ReadToEnd();
        }

        var lines = output.Trim().Split("\n");
        var freeMemoryParts = lines[0].Split("=", StringSplitOptions.RemoveEmptyEntries);
        var totalMemoryParts = lines[1].Split("=", StringSplitOptions.RemoveEmptyEntries);

        var metrics = new MemoryMetrics();
        metrics.Total = Math.Round(double.Parse(totalMemoryParts[1]) / 1024, 0);
        metrics.Free = Math.Round(double.Parse(freeMemoryParts[1]) / 1024, 0);
        metrics.Used = metrics.Total - metrics.Free;

        return metrics;
    }

    private MemoryMetrics GetUnixMetrics()
    {
        var output = "";

        var info = new ProcessStartInfo("free -m");
        info.FileName = "/bin/bash";
        info.Arguments = "-c \"free -m\"";
        info.RedirectStandardOutput = true;

        using (var process = Process.Start(info))
        {
            output = process.StandardOutput.ReadToEnd();
            Console.WriteLine(output);
        }

        var lines = output.Split("\n");
        var memory = lines[1].Split(" ", StringSplitOptions.RemoveEmptyEntries);

        var metrics = new MemoryMetrics();
        metrics.Total = double.Parse(memory[1]);
        metrics.Used = double.Parse(memory[2]);
        metrics.Free = double.Parse(memory[3]);

        return metrics;
    }
}

as for IRuntimeInformationService :至于IRuntimeInformationService

using System.Runtime.InteropServices;
public class RuntimeInformationService : IRuntimeInformationService
{
    public OSPlatform GetOsPlatform()
    {
        if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) return OSPlatform.OSX;
        if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return OSPlatform.Linux;
        if(RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD)) return OSPlatform.FreeBSD;
        if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return OSPlatform.Windows;

        return OSPlatform.Create("Unknown");
    }

    public bool IsUnix()
    {
        var isUnix = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
                     RuntimeInformation.IsOSPlatform(OSPlatform.Linux);

        return isUnix;
    }
    public bool IsWindows()
    {
        return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
    }
    public bool IsLinux()
    {
        return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
    }               
    public bool IsFreeBSD()
    {
        return RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
    }       
    public bool IsOSX()
    {
        return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
    }
    public string GetRuntimeIdentifier()
    {
        return RuntimeInformation.RuntimeIdentifier;
    }
    public Architecture GetProcessArchitecture()
    {
        return RuntimeInformation.ProcessArchitecture;
    }

    public Architecture GetOSArchitecture()
    {
        return RuntimeInformation.OSArchitecture;
    }
    
    public string GetOSDescription()
    {
        return RuntimeInformation.OSDescription;
    }       
    public string GetFrameworkDescription()
    {
        return RuntimeInformation.FrameworkDescription;
    }
    

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 .NET CORE 在 C# web 应用程序中获取当前的 CPU/RAM/磁盘使用情况? - How to get the current CPU/RAM/Disk usage in a C# web application using .NET CORE? 如何获得总 CPU 使用率(所有进程)? (C#) - How to get total CPU usage (all processes)? (C#) 在 C# 中获取当前 CPU、RAM 和磁盘驱动器的使用情况 - Get current CPU, RAM and Disk drive usage in C# 如何获取已安装的 RAM 和 CPU 详细信息 c#.Net Core? - How I can get the installed RAM and CPU details c# .Net Core? 如何在C#(托管代码)中获得* THREAD *的CPU使用率和/或RAM使用率? - How can I get CPU usage and/or RAM usage of a *THREAD* in C# (managed code)? 如何从c#应用程序获得远程linux机器的cpu和ram使用 - How can I get cpu and ram usage of remote linux machine from c# application 如何在 C# 中获取 CPU 使用率? - How to get the CPU Usage in C#? 如何获得CPU使用率作为taskmgr? C# - How to Get CPU Usage as taskmgr? C# C#:一个带有所有子对象的对象需要多少RAM? 获取对象所需的RAM列表? - C#: How much RAM does an object with all sub-objects need? Get list of needed RAM for object? 使用WMI获取C#中每个进程的CPU和RAM使用情况? - Get CPU and RAM usage for each process in C# using WMI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM