How to Get the amount of RAM available on the System in C#

How to get the amount of RAM available on the System in C# using System.Management.

In C#, you can get the amount of available RAM on a system using the Microsoft.VisualBasic.Devices namespace or by querying the system's performance counters.

How to Get the amount of RAM available on the System in C#

Open your Visual Studio, then create a new Console Application project to play demo.

If you want to use Microsoft.VisualBasic.Devices you need to add a reference to Microsoft.VisualBasic if it's not already included in your project, then use the ComputerInfo class to get the available physical memory.

The ComputerInfo class in the Microsoft.VisualBasic.Devices namespace provides information about the computer's memory.

// c# get ram size
ComputerInfo computerInfo = new ComputerInfo();
ulong availableMemory = computerInfo.AvailablePhysicalMemory;
Console.WriteLine($"Available Physical Memory: {availableMemory / 1024 / 1024} MB");

If you want to use PerformanceCounter you should add a reference to System.Diagnostics to your project, then use the PerformanceCounter class to get the available memory.

The PerformanceCounter class can be used to get various system performance metrics, including available memory.

PerformanceCounter availableMemory = new PerformanceCounter("Memory", "Available MBytes");
float availableMB = availableMemory.NextValue();
Console.WriteLine($"Available Physical Memory: {availableMB} MB");

If you want to get information about the ram available on your system, you can use the Win32_OperatingSystem WMI class.

You need to import System.Management to your project by right-clicking on your project, then select Add References.

visual studio add references

Openning your Program class, then modify your code as shown below.

// c# get total memory
static void Main(string[] args)
    ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
    ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(objectQuery);
    ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
    foreach (ManagementObject managementObject in managementObjectCollection)
        Console.WriteLine("Total Visible Memory: {0} KB", managementObject["TotalVisibleMemorySize"]);
        Console.WriteLine("Free Physical Memory: {0} KB", managementObject["FreePhysicalMemory"]);
        Console.WriteLine("Total Virtual Memory: {0} KB", managementObject["TotalVirtualMemorySize"]);
        Console.WriteLine("Free Virtual Memory: {0} KB", managementObject["FreeVirtualMemory"]);

Press F5 to run your project.

How to retrieve the RAM amount available on the System in WinForms with C#

TotalVisibleMemorySize: The amount of physical memory available for the operating system. This value does not necessarily indicate the actual physical memory capacity, but what is reported for the operating system is available to it.

FreePhysicalMemory: The amount of physical memory currently not in use and available.

TotalVirtualMemorySize: Capacity of virtual memory. Calculated by adding the total amount of RAM to the paging capacity, that is, adding the amount of memory in or aggregated by the computer system to the SizeStoredInPagingFiles property.

FreeVirtualMemory: Virtual memory capacity currently unused and available.