Find total physical memory on computer, .NET core 2.1, c#, Ubuntu

Viewed 4082

How do I find the total available physical memory on a computer specifically running Ubuntu and .NET core? Most solutions work only on full .NET and the following .NET core code snippet --

ManagementClass mc = new ManagementClass ("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject item in moc)
    m1 = Convert.ToUInt64(item.Properties["TotalPhysicalMemory"].Value);

works properly on Windows but returns 0 on non-Windows systems.

1 Answers

Currently you can't get Hardware information on all Platforms, this is also mentioned in this issue: https://github.com/dotnet/corefx/issues/22660

I think since .NET Core 2.1 you can use Process class to get process memory, as mentioned in this issue: https://github.com/dotnet/corefx/issues/26669


As an alternative, on Linux you could always access HW and OS information from files in /proc, for memory there is /proc/meminfo There you get MemTotal, MemFree and a lot of other information.

However this approach requires some file parsing.

Related