How to get the total memory occupied by the application?

Asked

Viewed 350 times

5

I know there’s a GC.GetTotalMemory, but it only shows the consumption of managed memory. It has how to discover the full use of the application?

2 answers

5


You’d have to take the file and have it inspected through the class Process.

Being able to identify the memory used is complicated. There are basically two measures. One is the private memory that only the application is using. But it is misleading because it does not include the memory that is allocated by other processes, including the operating system itself, because of its application. So taking the private memory does not represent the exact consumption.

But if you take the total working memory of the application, which includes all the memory that’s linked to the application, there’s another problem since that memory might be being used by other applications, then even though that amount of memory is actually being used, a part of it would be being used anyway even if the application was not running and with that memory linked to it.

    using static System.Console;
    using System.Diagnostics;
    
    public class Program {
        public static void Main() {
            using var proc = Process.GetCurrentProcess();
            WriteLine(proc.PrivateMemorySize64);
            WriteLine(proc.WorkingSet64);
        }
    }

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Note that the using is required for process release, otherwise there will be memory leakage.

4

You can use

using System.Diagnostics;

Process currentProc = Process.GetCurrentProcess();

long memoryUsed = currentProc.PrivateMemorySize64;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.