Skip to content

Instantly share code, notes, and snippets.

@ljchuello
Created December 22, 2022 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljchuello/00ea18ec527e2b8103ea4ad272511e61 to your computer and use it in GitHub Desktop.
Save ljchuello/00ea18ec527e2b8103ea4ad272511e61 to your computer and use it in GitHub Desktop.
Get Memory .NET Core Linux
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