For debugging purposes, I have written this little static method:
public static long CheckMemory(long maxMemorySizeBytes)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var usedMemoryBytes = Process.GetCurrentProcess().VirtualMemorySize64;
if (usedMemoryBytes > maxMemorySizeBytes)
Debugger.Break();
return usedMemoryBytes;
}
For some reason, VirtualMemorySize64
keeps returning lots more memory than what Visual Studio Diagnostic Tools window is showing, as well as what the Task Manager is showing. For the specific example I'm running now, here are the numbers:
Why are there such large differences and how do I properly track memory usage from within the app itself?
Search for Resource Monitor and click the top result to open the app. Click the Memory tab. Click the Private (KB) header to sort the applications by memory usage. (The information is presented in kilobytes.)
What is a CPU and memory usage monitor? A full-stack CPU and memory usage monitor serves to collect saturation metrics from your on-premises server. Saturation is one of the four golden signals of resource monitoring, and it allows you to understand when CPU utilization and memory are at or near capacity.
To check the amount of RAM that apps are using, follow these steps: Open Start. Search for Resource Monitor and click the top result to open the app. Click the Memory tab. Click the Private (KB) header to sort the applications by memory usage. (The information is presented in kilobytes.)
To determine which apps are using the most memory, use these steps: Open Start. Search for Task Manager and click the top result to open the app. Quick tip: You can also right-click the taskbar and select the Task Manager option, or right-click the Start button and select the Task Manager option.
VirtualMemorySize measures all of the virtual memory that your process uses. Which includes the pages shared by all other processes on your machine. Which in a .NET program includes the operating system, CLR, jitter and the ngen-ed Framework assemblies.
Diagnostic tools - Displays a live graph of your application’s Private Bytes metric. Private Bytes is a measure of the total amount of memory that a process has allocated, not including memory shared with other processes.
In Task Manager, by default, you see the "private working set" memory, which is the amount of memory used by a process that cannot be shared among other processes.
So:
If you want to get an idea of how much memory you're using, retrieve the VirtualMemorySize, Working Set and Private Bytes for a process.
If you add up all of the processes' VirtualMemorySize, you'll likely find it adds up to way more memory than you actually have. That's because those memory-mapped files, EXEs, DLLs, etc. can be shared among processes; and the same physical page in RAM can be accessed in more than one's process's address space at once.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With