C#获取CPU和内存使用率
2021-02-03 00:13
标签:读取 thread nfa 总计 链接 threading api owa param
方式1: 方式2: 备注:需要添加 System.Management 的引用 相关链接 : C#获取CPU和内存使用率 标签:读取 thread nfa 总计 链接 threading api owa param 原文地址:https://www.cnblogs.com/lonelyxmas/p/11525481.html获取内存使用率
using System;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
public class Class1
{
static void Main(string[] args)
{
Console.WriteLine("总内存:" + FormatSize(GetTotalPhys()));
Console.WriteLine("已使用:" + FormatSize(GetUsedPhys()));
Console.WriteLine("可使用:" + FormatSize(GetAvailPhys()));
Console.ReadKey();
}
#region 获得内存信息API
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GlobalMemoryStatusEx(ref MEMORY_INFO mi);
//定义内存的信息结构
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength; //当前结构体大小
public uint dwMemoryLoad; //当前内存使用率
public ulong ullTotalPhys; //总计物理内存大小
public ulong ullAvailPhys; //可用物理内存大小
public ulong ullTotalPageFile; //总计交换文件大小
public ulong ullAvailPageFile; //总计交换文件大小
public ulong ullTotalVirtual; //总计虚拟内存大小
public ulong ullAvailVirtual; //可用虚拟内存大小
public ulong ullAvailExtendedVirtual; //保留 这个值始终为0
}
#endregion
#region 格式化容量大小
///
using System;
using System.Management;
namespace ConsoleApp1
{
public class Class1
{
static void Main(string[] args)
{
//需要添加 System.Management 的引用
//获取总物理内存大小
ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
double available=0, capacity=0;
foreach (ManagementObject mo1 in moc1)
{
capacity += ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
}
moc1.Dispose();
cimobject1.Dispose();
//获取内存可用大小
ManagementClass cimobject2 = new ManagementClass("Win32_PerfFormattedData_PerfOS_Memory");
ManagementObjectCollection moc2 = cimobject2.GetInstances();
foreach (ManagementObject mo2 in moc2)
{
available += ((Math.Round(Int64.Parse(mo2.Properties["AvailableMBytes"].Value.ToString()) / 1024.0, 1)));
}
moc2.Dispose();
cimobject2.Dispose();
Console.WriteLine("总内存=" + capacity.ToString() + "G");
Console.WriteLine("可使用=" + available.ToString() + "G");
Console.WriteLine("已使用=" + ((capacity - available)).ToString() + "G," + (Math.Round((capacity - available) / capacity * 100, 0)).ToString() + "%");
Console.ReadKey();
}
}
}
获取CPU使用率
using System;
using System.Diagnostics;
namespace ConsoleApp1
{
public class Class1
{
static void Main(string[] args)
{
PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
Console.WriteLine("电脑CPU使用率:" + cpuCounter.NextValue() + "%");
Console.WriteLine("电脑可使用内存:" + ramCounter.NextValue() + "MB");
Console.WriteLine();
while (true)
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine("电脑CPU使用率:" + cpuCounter.NextValue() + " %");
Console.WriteLine("电脑可使用内存:" + ramCounter.NextValue() + "MB");
Console.WriteLine();
if ((int)cpuCounter.NextValue() > 80)
{
System.Threading.Thread.Sleep(1000 * 60);
}
}
}
}
}