private static void WriteMemoryStatus(TextWriter writer) { try { GlobalMemoryStatus memoryStatus = new GlobalMemoryStatus(); writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Memory Load: {0}%", memoryStatus.MemoryLoad)); writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Total Physical: {0} MB", (memoryStatus.TotalPhysical / 1048576))); writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Available Physical: {0} MB", (memoryStatus.AvailablePhysical / 1048576))); writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Total Page File: {0} MB", (memoryStatus.TotalPageFile / 1048576))); writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Available Page File: {0} MB", (memoryStatus.AvailablePageFile / 1048576))); writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Total Virtual: {0} MB", (memoryStatus.TotalVirtual / 1048576))); writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Available Virtual: {0} MB", (memoryStatus.AvailableVirtual / 1048576))); writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Available Extented Virtual: {0} MB", (memoryStatus.AvailableExtendedVirtual / 1048576))); } catch (Exception ex) { Debug.Write("Unexpected exception attempting to get global memory status: " + ex.ToString()); } }
public IEnumerable <MetricSample> GetSamples() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var(success, totalMemory, freeMemory) = GlobalMemoryStatus.GetTotalPhysAndAvailPhys(); if (!success || totalMemory == 0 || freeMemory == 0) { return(null); } return(new List <MetricSample>(2) { new MetricSample(FreeMemory, freeMemory), new MetricSample(TotalMemory, totalMemory) }); } if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { var retVal = new List <MetricSample>(); using (var sr = new StreamReader("/proc/meminfo")) { var hasMemFree = false; var hasMemTotal = false; var line = sr.ReadLine(); while (line != null || retVal.Count != 2) { //See: https://github.com/elastic/beats/issues/4202 if (line != null && line.Contains("MemAvailable:")) { var(suc, res) = GetEntry(line, "MemAvailable:"); if (suc) { retVal.Add(new MetricSample(FreeMemory, res)); } hasMemFree = true; } if (line != null && line.Contains("MemTotal:")) { var(suc, res) = GetEntry(line, "MemTotal:"); if (suc) { retVal.Add(new MetricSample(TotalMemory, res)); } hasMemTotal = true; } if (hasMemFree && hasMemTotal) { break; } line = sr.ReadLine(); } } ConsecutiveNumberOfFailedReads = 0; return(retVal); } (bool, ulong) GetEntry(string line, string name) { var nameIndex = line.IndexOf(name, StringComparison.Ordinal); if (nameIndex < 0) { return(false, 0); } var values = line.Substring(line.IndexOf(name, StringComparison.Ordinal) + name.Length); if (string.IsNullOrWhiteSpace(values)) { return(false, 0); } var items = values.Trim().Split(' '); switch (items.Length) { case 1 when ulong.TryParse(items[0], out var res): return(true, res); case 2 when items[1].ToLowerInvariant() == "kb" && ulong.TryParse(items[0], out var res): return(true, res * 1024);