Created
December 22, 2022 12:42
-
-
Save ljchuello/00ea18ec527e2b8103ea4ad272511e61 to your computer and use it in GitHub Desktop.
Get Memory .NET Core Linux
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
using System.Diagnostics; | |
namespace Prueba | |
{ | |
internal class Program | |
{ | |
public class MemoryMetrics | |
{ | |
public double Total; | |
public double Used; | |
public double Free; | |
} | |
static void Main(string[] args) | |
{ | |
MainAsync(args).GetAwaiter().GetResult(); | |
} | |
static async Task MainAsync(string[] args) | |
{ | |
try | |
{ | |
while (true) | |
{ | |
var output = ""; | |
var info = new ProcessStartInfo("free -m"); | |
info.FileName = "/bin/bash"; | |
info.Arguments = "-c \"free -m\""; | |
info.RedirectStandardOutput = true; | |
using (var process = Process.Start(info)) | |
{ | |
output = process.StandardOutput.ReadToEnd(); | |
Console.WriteLine(output); | |
} | |
var lines = output.Split("\n"); | |
var memory = lines[1].Split(" ", StringSplitOptions.RemoveEmptyEntries); | |
var metrics = new MemoryMetrics(); | |
metrics.Total = double.Parse(memory[1]); | |
metrics.Used = double.Parse(memory[2]) ; | |
metrics.Free = double.Parse(memory[3]); | |
metrics.Total = metrics.Total; | |
metrics.Used = metrics.Used; | |
metrics.Free = metrics.Total - metrics.Used; | |
Console.WriteLine(metrics.Total/1024); | |
Console.WriteLine(metrics.Used/1024); | |
Console.WriteLine(metrics.Free/1024); | |
await Task.Delay(1000); | |
} | |
Console.ReadLine(); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment