1

How can I get memory usage that I can see in Visual Studio (screenshot below) programmatically?

enter image description here

I've tried Process.GetCurrentProcess().WorkingSet64; but it's always less than actual memory I can see, when I hover process memory in VS. How can I do this in a proper way with C#?

Mehmet Topçu
  • 1
  • 1
  • 16
  • 31
SValley_Dev
  • 638
  • 4
  • 8
  • I don't exactly know, but VS might show you the _virtual_ memory space allocted for the process. If that's true, you might prefer `VirtualMemorySize64` over `WorkingSet64`, because the latter tells you only about the actual _physical_ RAM currently used by the procces. –  Aug 31 '22 at 11:51
  • The working set is the amount of physical RAM used, not the full memory used by a process, which includes pages swapped out to the page file. Even the working set [isn't accurate](https://stackoverflow.com/questions/5405693/help-understanding-windows-memory-working-set) because Windows uses several tricks to reduce swapping, like keeping swapped pages in a pool in case they're needed again. Which value you use depends on what you want to do. All are useful. Knowing the VM size helps track leaks but won't tell you that the app is slow because its pages got swapped out – Panagiotis Kanavos Aug 31 '22 at 12:05
  • @PanagiotisKanavos so how do I get the memory I see in Diagnostic tool or at least what kind of memory is shown in this Diagnostic tool? – SValley_Dev Aug 31 '22 at 12:33

1 Answers1

1

I recommend you to take a look at this article. GC.GetTotalMemory(Boolean) Method

It returns a number that is the best available approximation of the number of bytes currently allocated in managed memory.

Mehmet Topçu
  • 1
  • 1
  • 16
  • 31