是否有可能在C#.NET中获得系统可用内存的大小?如果是的话怎么样?
答案 0 :(得分:55)
使用Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory
。
右键单击您的项目,添加参考,选择Microsoft.VisualBasic
。
答案 1 :(得分:25)
这个答案是基于Hans Passant的。实际所需的属性是AvailablePhysicalMemory。它(和TotalPhysicalMemory等)是实例变量,所以它应该是
new ComputerInfo().AvailablePhysicalMemory
它适用于C#,但我想知道为什么this page说对于C#,“这种语言不受支持或者没有代码示例可用。”
答案 2 :(得分:17)
在Google搜索“c#系统内存”后从EggHeadCafe
您需要添加对System.Management的引用
using System;
using System.Management;
namespace MemInfo
{
class Program
{
static void Main(string[] args)
{
ObjectQuery winQuery = new ObjectQuery("SELECT * FROM Win32_LogicalMemoryConfiguration");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(winQuery);
foreach (ManagementObject item in searcher.Get())
{
Console.WriteLine("Total Space = " + item["TotalPageFileSpace"]);
Console.WriteLine("Total Physical Memory = " + item["TotalPhysicalMemory"]);
Console.WriteLine("Total Virtual Memory = " + item["TotalVirtualMemory"]);
Console.WriteLine("Available Virtual Memory = " + item["AvailableVirtualMemory"]);
}
Console.Read();
}
}
}
输出:
总空间= 4033036
总物理内存= 2095172
虚拟内存总量= 1933904
可用虚拟内存= 116280
答案 3 :(得分:7)
var performance = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
var memory = performance.NextValue();
答案 4 :(得分:4)
使用可通过System.Diagnostics访问的性能计数器将是一个选项。
参考http://www.dotnetspider.com/resources/4612-Find-Memory-usage-CPU-usage.aspx
希望这有帮助!
答案 5 :(得分:0)
一段代码:
System.Diagnostics.PerformanceCounter ramCounter;
ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available Bytes"); //"Available MBytes" for MB
string getAvailableRAMInBytes = ramCounter.NextValue() + "byte";