Skip to content

Instantly share code, notes, and snippets.

@wtarr
Created June 30, 2013 11:34
Show Gist options
  • Save wtarr/5894837 to your computer and use it in GitHub Desktop.
Save wtarr/5894837 to your computer and use it in GitHub Desktop.
Find percentage used physical memory - with aid of WMI code generator http://www.microsoft.com/en-ie/download/details.aspx?id=8572
using System;
using System.Management;
namespace WMIQuery
{
public class UsedPhysicalMemoryQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_OperatingSystem");
foreach (ManagementObject queryObj in searcher.Get())
{
double free = Double.Parse(queryObj["FreePhysicalMemory"].ToString());
double total = Double.Parse(queryObj["TotalVisibleMemorySize"].ToString());
Console.WriteLine("Percentage used: {0}%", Math.Round(((total - free)/total * 100), 2));
Console.Read();
}
}
catch (ManagementException e)
{
Console.WriteLine(value: e.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment