如何获取可用系统内存的大小?

问题描述 投票:0回答:7

可以在C#.NET中获取系统可用内存的大小?如果是,怎么?

c# .net memory diagnostics
7个回答
72
投票

使用

Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory

右键单击您的项目,添加参考,选择

Microsoft.VisualBasic

.


29
投票
这个答案是基于汉斯·帕斯特(Hans Passant)的。实际上,所需的属性实际上是可用的。它(以及全面的记忆及其他成员)是实例变量,因此应该为

new ComputerInfo().AvailablePhysicalMemory
它在C#中起作用,但我想知道为什么thispage

在C#上说:“不支持此语言或没有代码示例。”


20
投票
搜索后,从

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
thouttal物理内存=2095172

局部虚拟内存=1933904

可用的虚拟内存=116280

var performance = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes"); var memory = performance.NextValue();


14
投票
通过系统可以访问性能计数器。诊断是一个选项。
参考
http://www.dotnetspider.com/resources/4612-find-memory-usage-usage-cpu-usage.aspx

2
投票

希望这有帮助!

一块代码:

System.Diagnostics.PerformanceCounter ramCounter; ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available Bytes"); //"Available MBytes" for MB string getAvailableRAMInBytes = ramCounter.NextValue() + "byte";


1
投票

这里是使用GarbageCollector的选择,因此无需安装其他软件包:

var gcMemoryInfo = GC.GetGCMemoryInfo();
var installedMemoryKb = gcMemoryInfo.TotalAvailableMemoryBytes / 1024;
var usedMemoryKb = GC.GetTotalMemory(true) / 1024;
var availableMemoryKb = installedMemoryKb - usedMemoryKb;

0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.