獲取記憶體使用率 方式1: using System; using System.Runtime.InteropServices; namespace ConsoleApp1 { public class Class1 { static void Main(string[] args) { Conso ...
獲取記憶體使用率
方式1:
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 格式化容量大小 /// <summary> /// 格式化容量大小 /// </summary> /// <param name="size">容量(B)</param> /// <returns>已格式化的容量</returns> private static string FormatSize(double size) { double d = (double)size; int i = 0; while ((d > 1024) && (i < 5)) { d /= 1024; i++; } string[] unit = { "B", "KB", "MB", "GB", "TB" }; return (string.Format("{0} {1}", Math.Round(d, 2), unit[i])); } #endregion #region 獲得當前記憶體使用情況 /// <summary> /// 獲得當前記憶體使用情況 /// </summary> /// <returns></returns> public static MEMORY_INFO GetMemoryStatus() { MEMORY_INFO mi = new MEMORY_INFO(); mi.dwLength = (uint)System.Runtime.InteropServices.Marshal.SizeOf(mi); GlobalMemoryStatusEx(ref mi); return mi; } #endregion #region 獲得當前可用物理記憶體大小 /// <summary> /// 獲得當前可用物理記憶體大小 /// </summary> /// <returns>當前可用物理記憶體(B)</returns> public static ulong GetAvailPhys() { MEMORY_INFO mi = GetMemoryStatus(); return mi.ullAvailPhys; } #endregion #region 獲得當前已使用的記憶體大小 /// <summary> /// 獲得當前已使用的記憶體大小 /// </summary> /// <returns>已使用的記憶體大小(B)</returns> public static ulong GetUsedPhys() { MEMORY_INFO mi = GetMemoryStatus(); return (mi.ullTotalPhys - mi.ullAvailPhys); } #endregion #region 獲得當前總計物理記憶體大小 /// <summary> /// 獲得當前總計物理記憶體大小 /// </summary> /// <returns&gt;總計物理記憶體大小(B)&lt;/returns&gt; public static ulong GetTotalPhys() { MEMORY_INFO mi = GetMemoryStatus(); return mi.ullTotalPhys; } #endregion } }View Code
方式2:
備註:需要添加 System.Management 的引用
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(); } } }View Code
獲取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); } } } } }View Code
相關鏈接 :
- C#獲取電腦型號、系統版本、記憶體大小、硬碟大小、CPU信息
- 關於c# 中讀取系統記憶體大小的問題
- C#獲取特定進程CPU和記憶體使用率
- C#獲取CPU占用率、記憶體占用、磁碟占用、進程信息
- C# 準確獲取系統 CPU 使用率
- 如何用C#實時獲取CPU利用率
- C#中利用WMI對象獲取物理記憶體和可用記憶體大小信息
- C#獲取系統記憶體大小