Display memory usage in C#

[memory usage]

This program displays the computer’s total and free physical, virtual, and page file memory usage. When it starts, the following code displays the system’s memory information in the program’s ListView control.

// Prepare the ListView and display values.
private void Form1_Load(object sender, EventArgs e)
    // Make the columns.
    lvwInfo.View = View.Details;
    lvwInfo.SetColumnHeaders(new object[]
            "Property", HorizontalAlignment.Left,
            "Value", HorizontalAlignment.Right

    // Add the values.
    ManagementObjectSearcher os_searcher =
        new ManagementObjectSearcher(
            "SELECT * FROM Win32_OperatingSystem");
    foreach (ManagementObject mobj in os_searcher.Get())
        GetInfo(mobj, "FreePhysicalMemory");
        GetInfo(mobj, "FreeSpaceInPagingFiles");
        GetInfo(mobj, "FreeVirtualMemory");
        GetInfo(mobj, "SizeStoredInPagingFiles");
        GetInfo(mobj, "TotalSwapSpaceSize");
        GetInfo(mobj, "TotalVirtualMemorySize");
        GetInfo(mobj, "TotalVisibleMemorySize");

    // Size the columns.

This code sets the ListView control’s View property to show details. It then calls the SetColumnHeaders extension method to make the control display two column headers: Property and Value.

Next the program creates a ManagementObjectSearcher to get information from the Win32_OperatingSystem structure. It loops through the returned searchers (there will be only one) and calls the GetInfo method to get information about several of the searcher’s properties.

The code finishes by calling SizeColumnsToFitDataAndHeaders extension method to size the ListView control’s columns to fit its data and headers. (See Sort ListView controls by all columns or clicked columns in C# for information about the ListView extension methods.)

The following code shows the GetInfo method.

// Add information about the property to the ListView.
private void GetInfo(ManagementObject mobj, string property_name)
    object property_obj = mobj[property_name];
    if (property_obj == null)
        lvwInfo.AddRow(property_name, "???");
        ulong property_value = (ulong)property_obj * 1024;

This method gets property information from the ManagementObject by using the property’s name as an index. If the result is null, the code adds the property name and some question marks to the ListView control. If the object is not null, the code converts it into an unsigned long and displays the property name and its value in the ListView.

Note that the returned value is in KB so the code multiplies the result by 1024. The returned value is an unsigned long so it can handle values up to 18,446,744,073,709,551,615, which is more than 16 exabytes.

The code uses the ToFileSizeApi extension method to convert the long value into a memory size value such as KB, MB, GB, etc. (For information about that extension method, see Format file sizes in KB, MB, GB, and so forth in C#.)

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in memory, programs and tagged , , , , , , , , , , , . Bookmark the permalink.