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

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

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

Creating a new Console Application project. Next, you need to import System.Management to your project by right-clicking on your Project, then select Add References.

visual studio add references

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

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"]);
    }
    Console.ReadLine();
}

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.