Retrieve available memory from system.
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Wed, 9 Jul 2014 12:58:03 +0000 (14:58 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Wed, 9 Jul 2014 12:58:03 +0000 (14:58 +0200)
Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj
Allgemeines/FreeCSharp/FreeCSharp/Program.cs

index f577e1e..b3b475a 100644 (file)
@@ -19,8 +19,8 @@
     <DefineConstants>DEBUG;</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
-    <Externalconsole>true</Externalconsole>
     <PlatformTarget>x86</PlatformTarget>
+    <ConsolePause>false</ConsolePause>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
     <DebugType>full</DebugType>
@@ -33,6 +33,7 @@
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
+    <Reference Include="Mono.Posix" />
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Program.cs" />
index 7f2817c..1aa3e6f 100644 (file)
@@ -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);
+            }
         }
     }