How to get the size of available system memory?

C#.NetMemoryDiagnostics

C# Problem Overview


Is it possible to get the size of system available memory in C#.NET? if yes how?

C# Solutions


Solution 1 - C#

Use Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory.

Right-click your project, Add Reference, select Microsoft.VisualBasic.

Solution 2 - C#

This answer is based on Hans Passant's. The required property is AvailablePhysicalMemory actually. and it (and TotalPhysicalMemory and others) are instance variables, so it should be

new ComputerInfo().AvailablePhysicalMemory

It works in C#, but I wonder why this page says that for C#, "This language is not supported or no code example is available."

Solution 3 - C#

From EggHeadCafe after googling for 'c# system memory'

You will need to add a reference to System.Management

using System;
using System.Management;

namespace MemInfo
{
    class Program
    {       
        static void Main(string[] args)
        {
            ObjectQuery winQuery = new ObjectQuery("SELECT * FROM Win32_LogicalMemoryConfiguration");

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(winQuery);

            foreach (ManagementObject item in searcher.Get())
            {
                Console.WriteLine("Total Space = " + item["TotalPageFileSpace"]);
                Console.WriteLine("Total Physical Memory = " + item["TotalPhysicalMemory"]);
                Console.WriteLine("Total Virtual Memory = " + item["TotalVirtualMemory"]);
                Console.WriteLine("Available Virtual Memory = " + item["AvailableVirtualMemory"]);
            }
            Console.Read();
        }
    }
}

Output:

Total Space = 4033036

Total Physical Memory = 2095172

Total Virtual Memory = 1933904

Available Virtual Memory = 116280

Solution 4 - C#

var performance = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
var memory = performance.NextValue();

Solution 5 - C#

Using the performance counters accessible via System.Diagnostics will be one option.

Refer http://www.dotnetspider.com/resources/4612-Find-Memory-usage-CPU-usage.aspx

Hope this helps!

Solution 6 - C#

A piece of codes:

 System.Diagnostics.PerformanceCounter ramCounter;     
 ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available Bytes"); //"Available MBytes" for MB
 string getAvailableRAMInBytes = ramCounter.NextValue() + "byte";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLouis RhysView Question on Stackoverflow
Solution 1 - C#Hans PassantView Answer on Stackoverflow
Solution 2 - C#Louis RhysView Answer on Stackoverflow
Solution 3 - C#TheEvilPenguinView Answer on Stackoverflow
Solution 4 - C#vikingfabianView Answer on Stackoverflow
Solution 5 - C#JagmagView Answer on Stackoverflow
Solution 6 - C#JonneyView Answer on Stackoverflow