You can use this implementation of the "free" command (UNIX) to search for used and available physical memory (this is the best IMHO option):
using System; using System.Text.RegularExpressions; using System.IO; namespace FreeCSharp { class MainClass { public static void Main(string[] args) { FreeCSharp free = new FreeCSharp(); free.GetValues(); long buffersPlusCached = free.Buffers + free.Cached; long mainUsed = free.MemTotal - free.MemFree;
Alternatively, you can use sysconf (UNIX) to get the pages of physical memory that are currently available. This value depends on how much data is cached by the OS, because of this you must do echo 3 > /proc/sys/vm/drop_caches before running this code:
using System; using Mono.Unix.Native; OperatingSystem os = Environment.OSVersion; PlatformID pid = os.Platform; if (pid == PlatformID.Unix || pid == PlatformID.MacOSX) { long pages = Syscall.sysconf (SysconfName._SC_AVPHYS_PAGES); long page_size = Syscall.sysconf (SysconfName._SC_PAGESIZE); Console.WriteLine("The number of currently available pages of physical memory: {0}, Size of a page in bytes: {1} bytes", pages, page_size); Console.WriteLine("Mem: {0} bytes", pages * page_size); }
source share