BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 1.1+

Getting Free and Total Memory Details

Some software monitors the amount of physical or virtual memory that is available to the operating system, or obtains this information before attempting processes that will use large amounts of RAM. Total and available memory can be read using WMI.

Win32_OperatingSystem

Windows Management Instrumentation (WMI) allows you to access information relating to the operating system, registry, hardware and installed software. It also permits you to modify operating system settings and execute some actions, either on a local or remote machine.

One useful WMI class is Win32_OperatingSystem. You can use it to represent the Windows operating system installed on a computer. If a machine has multiple operating systems installed, the class provides details for the active one. You can use Win32_OperatingSystem to obtain the amount of memory available to the operating system. The class provides four properties for this purpose, each returning a value measured in kilobytes:

  • TotalVisibleMemorySize. Returns the total amount of physical memory that is installed in the computer and accessible to the operating system. This may be a lower number than is installed if some memory is not addressable.
  • FreePhysicalMemory. Returns the amount of physical memory that is currently not in use.
  • TotalVirtualMemorySize. Returns the total amount of virtual memory available to the operating system. This includes the visible physical memory and the total paging space.
  • FreeVirtualMemory. Returns the amount of virtual memory not currently in use.

NB: The Win32_LogicalMemoryConfiguration WMI class also provides details of free and total memory. However, this class is only supported for use with Windows 2000.

Reading the Memory Properties

The code below demonstrates reading the above properties using a WQL query in a C# console application. The query obtains all operating system data accessible via Win32_OperatingSystem. The results contain one row, which holds the details of the memory for the local computer's active operating system. The output will vary according to your system configuration and executing programs.

ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();

foreach (ManagementObject result in results)
{
    Console.WriteLine("Total Visible Memory: {0}KB", result["TotalVisibleMemorySize"]);
    Console.WriteLine("Free Physical Memory: {0}KB", result["FreePhysicalMemory"]);
    Console.WriteLine("Total Virtual Memory: {0}KB", result["TotalVirtualMemorySize"]);
    Console.WriteLine("Free Virtual Memory: {0}KB", result["FreeVirtualMemory"]);
}

/* OUTPUT
                  
Total Visible Memory: 12581368KB
Free Physical Memory: 10370644KB
Total Virtual Memory: 25160892KB
Free Virtual Memory: 22699568KB
                  
*/
5 March 2011