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
.

- 922,412
- 146
- 1,693
- 2,536
-
7It is a .NET framework class, available in any version since 2.0. The namespace name is irrelevant. – Hans Passant Jul 21 '10 at 05: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. – atconway Jul 16 '13 at 18:43
-
8If you don't like using the VB wrapper, you can wrap the same native method it uses: http://pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html – EricLaw Aug 03 '15 at 20:32
-
2I'am on C# and .NET 4.5.2: `new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory` – guneysus Jul 07 '16 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. – Curtis Aug 19 '19 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."

- 34,517
- 56
- 153
- 221
-
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 – Piotr Kula Sep 15 '12 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

- 98,941
- 38
- 226
- 299

- 5,634
- 1
- 26
- 47
-
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. – Canacourse Jul 12 '11 at 15:57
-
3Following on from @Canacourse's comment, see: http://msdn.microsoft.com/en-us/library/aa387937(v=vs.85).aspx for information on the CIM_OperatingSystem class. – Brett Rigby Aug 05 '13 at 09:28
var performance = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
var memory = performance.NextValue();

- 451
- 5
- 5
-
1FYI "Access to the registry key 'Global' is denied" happens unless you explicitly grant permission. – CmdrTallen Jul 30 '18 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!

- 10,283
- 1
- 34
- 58
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";

- 91
- 6
-
Hey! I like your code example much more, as the variable names are more "talkative" ;-) – Xan-Kun Clark-Davis Jul 05 '22 at 13:35