How to get memory available or used in C#

C#Memory Management

C# Problem Overview


How can I get the available RAM or memory used by the application?

C# Solutions


Solution 1 - C#

You can use:

Process proc = Process.GetCurrentProcess();

To get the current process and use:

proc.PrivateMemorySize64;

To get the private memory usage. For more information look at this link.

Solution 2 - C#

You might want to check the GC.GetTotalMemory method.

It retrieves the number of bytes currently thought to be allocated by the garbage collector.

Solution 3 - C#

System.Environment has WorkingSet- a 64-bit signed integer containing the number of bytes of physical memory mapped to the process context.

In .NET Core 3.0 and later (aka .NET 5 and later), you can use GC.GetGCMemoryInfo to get information about memory used by the GC heap and how much memory the GC thinks is available. .NET internally uses this data to calculate memory pressure. The memory pressure is used to decide when to trim the System.Buffers.ArrayPool.

Solution 4 - C#

In addition to @JesperFyhrKnudsen's answer and @MathiasLykkegaardLorenzen's comment, you'd better dispose the returned Process after using it.

So, In order to dispose the Process, you could wrap it in a using scope or calling Dispose on the returned process (proc variable).

  1. using scope:

    var memory = 0.0;
    using (Process proc = Process.GetCurrentProcess())
    {
        // The proc.PrivateMemorySize64 will returns the private memory usage in byte.
        // Would like to Convert it to Megabyte? divide it by 2^20
           memory = proc.PrivateMemorySize64 / (1024*1024);
    }
    
  2. Or Dispose method:

    var memory = 0.0;
    Process proc = Process.GetCurrentProcess();
    memory = Math.Round(proc.PrivateMemorySize64 / (1024*1024), 2);
    proc.Dispose();
    

Now you could use the memory variable which is converted to Megabyte.

Solution 5 - C#

Look here for details.

private PerformanceCounter cpuCounter;
private PerformanceCounter ramCounter;
public Form1()
{
	InitializeComponent();
	InitialiseCPUCounter();
	InitializeRAMCounter();
	updateTimer.Start();
}

private void updateTimer_Tick(object sender, EventArgs e)
{
	this.textBox1.Text = "CPU Usage: " +
	Convert.ToInt32(cpuCounter.NextValue()).ToString() +
	"%";

	this.textBox2.Text = Convert.ToInt32(ramCounter.NextValue()).ToString()+"Mb";
}

private void Form1_Load(object sender, EventArgs e)
{
}

private void InitialiseCPUCounter()
{
	cpuCounter = new PerformanceCounter(
	"Processor",
	"% Processor Time",
	"_Total",
	true
	);
}

private void InitializeRAMCounter()
{
	ramCounter = new PerformanceCounter("Memory", "Available MBytes", true);
 
}

If you get value as 0 it need to call NextValue() twice. Then it gives the actual value of CPU usage. See more details here.

Solution 6 - C#

For the complete system you can add the Microsoft.VisualBasic Framework as a reference;

 Console.WriteLine("You have {0} bytes of RAM",
        new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory);
        Console.ReadLine();

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
Questionuser34537View Question on Stackoverflow
Solution 1 - C#Jesper Fyhr KnudsenView Answer on Stackoverflow
Solution 2 - C#Christian C. SalvadóView Answer on Stackoverflow
Solution 3 - C#AustinView Answer on Stackoverflow
Solution 4 - C#Ahmad PayanView Answer on Stackoverflow
Solution 5 - C#DevTView Answer on Stackoverflow
Solution 6 - C#PodTech.ioView Answer on Stackoverflow