From a27f8f4fa8810d8a16987352b7a587c7779b6eaf Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Wed, 9 Jul 2014 14:58:03 +0200 Subject: [PATCH] Retrieve available memory from system. --- .../FreeCSharp/FreeCSharp/FreeCSharp.csproj | 3 +- Allgemeines/FreeCSharp/FreeCSharp/Program.cs | 42 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj b/Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj index f577e1e..b3b475a 100644 --- a/Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj +++ b/Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj @@ -19,8 +19,8 @@ DEBUG; prompt 4 - true x86 + false full @@ -33,6 +33,7 @@ + diff --git a/Allgemeines/FreeCSharp/FreeCSharp/Program.cs b/Allgemeines/FreeCSharp/FreeCSharp/Program.cs index 7f2817c..1aa3e6f 100644 --- a/Allgemeines/FreeCSharp/FreeCSharp/Program.cs +++ b/Allgemeines/FreeCSharp/FreeCSharp/Program.cs @@ -1,6 +1,8 @@ using System; using System.Text.RegularExpressions; using System.IO; +using Mono.Unix.Native; +using System.Diagnostics; namespace FreeCSharp { @@ -8,6 +10,8 @@ namespace FreeCSharp { public static void Main(string[] args) { + Console.WriteLine (); + Console.WriteLine ("USING free C#, just UNIX"); FreeCSharp free = new FreeCSharp(); free.GetValues(); @@ -15,11 +19,47 @@ namespace FreeCSharp long mainUsed = free.MemTotal - free.MemFree; // What you would get from free command: - Console.WriteLine("-/+ buffers/cache: {0} {1}", (mainUsed - buffersPlusCached), (free.MemFree + buffersPlusCached)); + Console.WriteLine("-/+ buffers/cache: {0} {1}", + (mainUsed - buffersPlusCached), (free.MemFree + buffersPlusCached)); // What means: Console.WriteLine("Used physical memory: {0} kB", mainUsed - buffersPlusCached); Console.WriteLine("Available physical memory: {0} kB", free.MemFree + buffersPlusCached); + + Console.WriteLine (); + Console.WriteLine ("USING SYSCALL, just UNIX"); + 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); + } + + if (pid == PlatformID.Unix || pid == PlatformID.MacOSX) { + long pages = Syscall.sysconf (SysconfName._SC_PHYS_PAGES); + long page_size = Syscall.sysconf (SysconfName._SC_PAGESIZE); + Console.WriteLine("The number of pages of physical memory: {0}, " + + "Size of a page in bytes: {1} bytes", pages, page_size); + Console.WriteLine("Mem: {0} bytes", pages * page_size); + } + + + Console.WriteLine (); + Console.WriteLine ("USING PerformanceCounter, platform independent"); + string categoryName = "Mono Memory"; + string counterName = "Total Physical Memory"; + + try { + var pc = new PerformanceCounter (categoryName, counterName); + Console.WriteLine ("Value of performance counter '{0}/{1}': {2}", + categoryName, counterName, pc.RawValue); + } catch (InvalidOperationException ex) { + Console.WriteLine ("Category name '{0}' does not exist. {1}", + categoryName, ex.Message); + } } } -- 2.1.4