I know there's GC.GetTotalMemory
, but it only shows the consumption of managed memory. Can you find full application usage?
I know there's GC.GetTotalMemory
, but it only shows the consumption of managed memory. Can you find full application usage?
You would have to pick up the process and have it inspected through the Process
.
Getting the memory used is somewhat 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 getting the private memory does not give the exact consumption.
But if you take the total working memory of the application which includes all memory that is bound to the application has another problem since this memory may be used by other applications, so although this amount of memory is actually being used, a part of it would be being used anyway even though the application was not running and with that memory bound 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);
}
}
}
See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .
Note that using
is required for process release, otherwise memory leak.
You can use
using System.Diagnostics;
Process currentProc = Process.GetCurrentProcess();
long memoryUsed = currentProc.PrivateMemorySize64;