Is it possible to get the size of system available memory in C#.NET? if yes how?
6 Answers
Use Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory
.
Right-click your project, Add Reference, select Microsoft.VisualBasic
.
-
7It is a .NET framework class, available in any version since 2.0. The namespace name is irrelevant. Jul 21, 2010 at 5:26
-
32@JeffreyKevinPry - It doesn't mean your doing VB.NET or anything. It's just another useful Framework library that in this case has wrapped up functionality that is appropriate to use. It's not crossing any C# purist line; it's just using the full potential of .NET.– atconwayJul 16, 2013 at 18:43
-
8If you don't like using the VB wrapper, you can wrap the same native method it uses: pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html– EricLawAug 3, 2015 at 20:32
-
2I'am on C# and .NET 4.5.2:
new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory
– guneysusJul 7, 2016 at 14:27 -
For C#, just add a reference to your project for Microsoft.VisualBasic. Then, add using Microsoft.VisualBasic.Devices; To the top of the file where you'll be using it. Then, you can use it as new ComputerInfo().AvailablePhysicalMemory.– CurtisAug 19, 2019 at 23:48
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
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."
-
3No- because its part of the
My
Namespace used in Visual Basic! Not C# - Including this Visual Basic dll will expose these short cuts that are so nice in VB.NET; And people who go into C# after VB start to miss that. Nice answer +1 Sep 15, 2012 at 14:59 -
From EggHeadCafe after googling for 'c# system memory'
You will need to add a reference to 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
Total Physical Memory = 2095172
Total Virtual Memory = 1933904
Available Virtual Memory = 116280
-
27Win32_LogicalMemoryConfiguration is not available from Vista onwards. Replace with CIM_OperatingSystem and use TotalVisibleMemorySize, TotalVirtualMemorySize etc. instead. The code above throws an exception on Win7 and probably on Vista as well. Jul 12, 2011 at 15:57
-
3Following on from @Canacourse's comment, see: msdn.microsoft.com/en-us/library/aa387937(v=vs.85).aspx for information on the CIM_OperatingSystem class. Aug 5, 2013 at 9:28
var performance = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
var memory = performance.NextValue();
-
1FYI "Access to the registry key 'Global' is denied" happens unless you explicitly grant permission. Jul 30, 2018 at 13:57
Using the performance counters accessible via System.Diagnostics will be one option.
Refer http://www.dotnetspider.com/resources/4612-Find-Memory-usage-CPU-usage.aspx
Hope this helps!
A piece of codes:
System.Diagnostics.PerformanceCounter ramCounter;
ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available Bytes"); //"Available MBytes" for MB
string getAvailableRAMInBytes = ramCounter.NextValue() + "byte";
-
Hey! I like your code example much more, as the variable names are more "talkative" ;-) Jul 5, 2022 at 13:35