-
-
Save sk8tz/93565ebbb3333b44de4d58a30d66f9ad to your computer and use it in GitHub Desktop.
C# WMI memory usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @yasinkuyu | |
// 06/05/2014 | |
public dynamic get_mem() | |
{ | |
dynamic returndata = new ExpandoObject(); | |
var search = new ManagementObjectSearcher("root\\CIMV2", "Select TotalVisibleMemorySize, FreePhysicalMemory from Win32_OPeratingSystem"); | |
foreach (var x in search.Get()) | |
{ | |
var totalMemory = (ulong)x["TotalVisibleMemorySize"]; | |
var freeMemory = (ulong)x["FreePhysicalMemory"]; | |
// -> KB to MB | |
var totalmem = (double)totalMemory / 1024; ; | |
var freemem = Convert.ToInt32(Math.Round(Convert.ToDecimal((((double)freeMemory / 1024))))); | |
var usedmem = Convert.ToInt32(Math.Round(Convert.ToDecimal((((totalmem - freemem)))))); | |
// Multiple return object | |
returndata.total = totalmem; | |
returndata.usage = usedmem; | |
returndata.free = freemem; | |
} | |
return returndata; | |
} | |
Sample | |
var memUsage = get_mem.usage; | |
Output | |
4726 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment