174

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

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
  • 3
    Note, 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
  • 1
    You 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 Answers6

211

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.

janw
  • 8,758
  • 11
  • 40
  • 62
Jesper Fyhr Knudsen
  • 7,802
  • 2
  • 35
  • 46
  • 65
    It 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
    Namespace: System.Diagnostics Assembly: System (in System.dll) – Enigma Plus May 20 '15 at 08:17
  • 14
    I'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
  • 1
    See 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
45

You might want to check the GC.GetTotalMemory method.

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

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 21
    Only in managed heaps though. Arkain's answer should give both native and managed heaps. – Yaur Feb 20 '13 at 19:29
32

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.

Austin
  • 2,771
  • 1
  • 18
  • 14
22

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.

AhmadReza Payan
  • 2,171
  • 1
  • 23
  • 33
14

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.

Austin
  • 2,771
  • 1
  • 18
  • 14
DevT
  • 4,843
  • 16
  • 59
  • 92
0

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();
PodTech.io
  • 4,874
  • 41
  • 24