How can I get the available RAM or memory used by the application?
-
3Note, physical RAM currently used is the working set, memory allocated falls into private or shared bytes (depending on the type of allocation). – Richard Apr 15 '09 at 10:01
-
1You should take a look at the [`System.Diagnostics.Process`](http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx) class. – Ronald Wildenberg Apr 15 '09 at 06:59
6 Answers
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.

- 8,758
- 11
- 40
- 62

- 7,802
- 2
- 35
- 46
-
65It should probably be noted that a call to GetCurrentProcess will itself allocate quite a lot of resources. Call Dispose on the returned process when done, or wrap the whole code in a "using" scope. – Mathias Lykkegaard Lorenzen Apr 01 '14 at 05:23
-
11
-
14I'd like to also add that the PrivateMemorySize64 property (+ other properties) itself is not automatically updated until Refresh() is called. (It's mentioned on the page at the link above.) – ScottRhee Jul 01 '15 at 21:34
-
1See this other similar question for more answers: http://stackoverflow.com/questions/14032515/how-to-get-the-amount-of-memory-used-by-an-application – Aaron D Nov 08 '15 at 16:20
-
1@ScottRhee correct me if i'm wrong - but I'd add that the property values are "up to date" at the moment that you call `GetCurrentProcess`, but only need to be refreshed if you are doing other operations prior to accessing the properties. [Source](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.refresh?view=netframework-4.8) - **Remarks** say the properties are correct at moment the process snapshot is created. – tabjsina Dec 17 '19 at 04:57
-
If my C# application is running in 32bit (for reasons I don't want to get into), do I still check PrivateMemorySize64 or should I be checking PrivateMemorySize? – scott.korin May 25 '22 at 17:54
-
1@scott.korin you should still use PrivateMemorySize64, the 64 refers to it's type which is a long, PrivateMemorySize only return an int. – Jesper Fyhr Knudsen Aug 25 '22 at 12:47
You might want to check the GC.GetTotalMemory method.
It retrieves the number of bytes currently thought to be allocated by the garbage collector.

- 807,428
- 183
- 922
- 838
-
21Only in managed heaps though. Arkain's answer should give both native and managed heaps. – Yaur Feb 20 '13 at 19:29
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.

- 2,771
- 1
- 18
- 14
-
Can you (or someone at all) give an example of how to use PerformanceCounter to get the system available/used memory? – The Elemental of Destruction Dec 13 '19 at 04:36
-
I removed the reference to PerformanceCounter, as it was not helpful. I added some information about GC.GetGCMemoryInfo, which is probably more useful. – Austin Dec 20 '21 at 03:40
-
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).
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); }
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.

- 2,171
- 1
- 23
- 33
-
3
-
3One note. In C# `^` is bitwise XOR, not power. So just use `proc.PrivateMemorySize64 / (1024*1024)`, or `proc.PrivateMemorySize64 / (1 << 20)` – Сергей Рыбаков May 18 '20 at 06:15
-
1You missed parenthesis, should be `proc.PrivateMemorySize64 / (1024 * 1024)` as multiplication does not have a priority over division. – Laurynas Lazauskas Jun 13 '20 at 14:05
-
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.
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();

- 4,874
- 41
- 24