Code is needed that returns in bytes the current application memory as in Task Manager.

The comments below lead to the following result:

enter image description here

  • one
    (int)System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 seems to be returning bytes in bytes - Alexey Shimansky
  • returned 110870528, in the dispatcher 46Mb - Dmitry Chistik
  • The result can be cached, so before taking the value you need to update it again Process myProcess = Process.Start("NotePad.exe"); do { if (!myProcess.HasExited) { myProcess.Refresh(); Console.WriteLine(" physical memory usage: {0}", myProcess.WorkingSet64); } } while (!myProcess.WaitForExit(1000)); Process myProcess = Process.Start("NotePad.exe"); do { if (!myProcess.HasExited) { myProcess.Refresh(); Console.WriteLine(" physical memory usage: {0}", myProcess.WorkingSet64); } } while (!myProcess.WaitForExit(1000)); - Alexey Shimansky
  • Unfortunately the same result - Dmitri Chistik
  • Constantly adds 40MB. Not critical, I just need to monitor memory growth. But the precipitate is ... - Dmitry Chistik

1 answer 1

If you need exactly the value shown in Task Manager, then you can subtract it from the performance counters:

 using System; using System.Diagnostics; class Program { static void Main(string[] args) { string prcName = Process.GetCurrentProcess().ProcessName; var counter = new PerformanceCounter("Process", "Working Set - Private", prcName); Console.WriteLine("{0}K", counter.RawValue / 1024); Console.ReadLine(); } } 

In fact, the Working Set - Private is not really "the amount of memory allocated to the process." By itself, the Working Set is the part of virtual memory that the process has recently accessed and which still corresponds to physical memory. And Working Set - Private is an inseparable part of Working Set.

Those. real consumption of memory, this indicator has a very indirect relationship, because depends not only on the memory allocation in the program, but also on how long the program has been accessing it, and on the general state of memory in the system.

  • Thanks, what you need - Dmitry Chistik