/// <summary> /// Returns the current memory information as parsed from /proc/meminfo. /// </summary> private static MemoryInfo LinuxMemoryInfo() { var mi = new MemoryInfo(); // Read in all of the lines from the meminfo file. var lines = File.ReadAllLines("/proc/meminfo"); // Go through each of them, clean them up and keep the ones we want. foreach (string line in lines) { // Cleanup our values. The first item here will be the key, the second item will // by the value. var items = line.Split(':'); items[1] = items[1].Replace(" ", "").Replace("kB", ""); if (items[0] == "MemTotal") { mi.SystemTotal = long.Parse(items[1]); } else if (items[0] == "MemFree") { mi.SystemFree = long.Parse(items[1]); } else if (items[0] == "MemAvailable") { mi.SystemAvailable = long.Parse(items[1]); } } mi.SystemUsed = mi.SystemTotal - mi.SystemFree; mi.ProcessUsed = Process.GetCurrentProcess().WorkingSet64 / 1024; return(mi); }
/// <summary> /// Returns the current memory information. Framework objects are used where they exist. The Windows API will /// where they do not exist. /// </summary> private static MemoryInfo WindowsMemoryInfo() { var mem = new MEMORYSTATUSEX(); mem.Init(); if (!GlobalMemoryStatusEx(ref mem)) { throw new Win32Exception("Could not obtain memory information due to internal error."); } var mi = new MemoryInfo { ProcessUsed = Process.GetCurrentProcess().WorkingSet64 / 1024, SystemTotal = mem.ullTotalPhys, SystemFree = mem.ullAvailPhys, SystemAvailable = mem.ullAvailPhys, VirtualTotal = mem.ullTotalVirtual, VirtualAvailable = mem.ullAvailVirtual, PageFileTotal = mem.ullTotalPageFile, PageFileAvailable = mem.ullAvailPageFile, MemoryLoad = mem.dwMemoryLoad, ExtendedVirtualMemoryAvailable = mem.ullAvailExtendedVirtual }; mi.SystemUsed = mi.SystemTotal - mi.SystemFree; return(mi); }