Is it possible to get the size of system available memory in C#.NET? if yes how?
是否有可能在C#.NET中获得系统可用内存的大小?如果有,怎么样?
5 个解决方案
#1
53
Use Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory
.
使用Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory。
Right-click your project, Add Reference, select Microsoft.VisualBasic
.
右键单击项目“添加引用”,选择“Microsoft.VisualBasic”。
#2
23
This answer is based on Hans Passant's. The required property is AvailablePhysicalMemory actually. and it (and TotalPhysicalMemory and others) are instance variables, so it should be
这个答案是基于Hans Passant的。实际所需的属性是AvailablePhysicalMemory。它(和TotalPhysicalMemory等)是实例变量,所以它应该是
new ComputerInfo().AvailablePhysicalMemory
It works in C#, but I wonder why this page says that for C#, "This language is not supported or no code example is available."
它适用于C#,但我想知道为什么这个页面说C#,“这种语言不受支持或者没有代码示例可用。”
#3
17
From EggHeadCafe after googling for 'c# system memory'
用谷歌搜索'c#系统内存'后从EggHeadCafe
You will need to add a reference to System.Management
您需要添加对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();
}
}
}
Output:
输出:
Total Space = 4033036
总空间= 4033036
Total Physical Memory = 2095172
总物理内存= 2095172
Total Virtual Memory = 1933904
总虚拟内存= 1933904
Available Virtual Memory = 116280
可用虚拟内存= 116280
#4
8
var performance = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
var memory = performance.NextValue();
#5
4
Using the performance counters accessible via System.Diagnostics will be one option.
使用可通过System.Diagnostics访问的性能计数器将是一种选择。
Refer http://www.dotnetspider.com/resources/4612-Find-Memory-usage-CPU-usage.aspx
请参阅http://www.dotnetspider.com/resources/4612-Find-Memory-usage-CPU-usage.aspx
Hope this helps!
希望这可以帮助!
#1
53
Use Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory
.
使用Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory。
Right-click your project, Add Reference, select Microsoft.VisualBasic
.
右键单击项目“添加引用”,选择“Microsoft.VisualBasic”。
#2
23
This answer is based on Hans Passant's. The required property is AvailablePhysicalMemory actually. and it (and TotalPhysicalMemory and others) are instance variables, so it should be
这个答案是基于Hans Passant的。实际所需的属性是AvailablePhysicalMemory。它(和TotalPhysicalMemory等)是实例变量,所以它应该是
new ComputerInfo().AvailablePhysicalMemory
It works in C#, but I wonder why this page says that for C#, "This language is not supported or no code example is available."
它适用于C#,但我想知道为什么这个页面说C#,“这种语言不受支持或者没有代码示例可用。”
#3
17
From EggHeadCafe after googling for 'c# system memory'
用谷歌搜索'c#系统内存'后从EggHeadCafe
You will need to add a reference to System.Management
您需要添加对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();
}
}
}
Output:
输出:
Total Space = 4033036
总空间= 4033036
Total Physical Memory = 2095172
总物理内存= 2095172
Total Virtual Memory = 1933904
总虚拟内存= 1933904
Available Virtual Memory = 116280
可用虚拟内存= 116280
#4
8
var performance = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
var memory = performance.NextValue();
#5
4
Using the performance counters accessible via System.Diagnostics will be one option.
使用可通过System.Diagnostics访问的性能计数器将是一种选择。
Refer http://www.dotnetspider.com/resources/4612-Find-Memory-usage-CPU-usage.aspx
请参阅http://www.dotnetspider.com/resources/4612-Find-Memory-usage-CPU-usage.aspx
Hope this helps!
希望这可以帮助!