EDIT: The bounty's expired, but if the community would like to award it to someone, then I choose Raful Chizkiyahu.
I have a memory leak in one of my C# Winforms programs, and I'd like to graph its memory usage over time to get a better understanding of what might be causing the leak. Problem is, none of the usual memory diagnostic commands match up with what the Task Manager is claiming as its consumed memory for that process. I assumed this was perhaps due to the app using unsafe/unmanaged code not being included in the total.
So to try and drill deeper, I created a new Winforms app, very simple, with just a timer to report the memory usage in realtime. I use 5 labels, each with various functions (mostly found from here): Environment.WorkingSet
, GC.GetTotalMemory(false)
, GC.GetTotalMemory(true)
, Process.GetCurrentProcess().PrivateMemorySize64
and Process.GetCurrentProcess().WorkingSet64
.
Surprisingly (or maybe not so, sigh), I still can't get any of these five numbers to match up with the Windows 10 Task Manager. Here's a screenshot:
So what I'm basically looking for is that 5.1MB number. How do I sneakily extract that hidden number from the .NET framework?
Here's the code I have in the Timer tick function:
private void timer1_Tick(object sender, EventArgs e)
{
//Refresh();
label1.Text = "Environment.WorkingSet: " + Environment.WorkingSet ;
label2.Text = "GC.GetTotalMemory(false): " + GC.GetTotalMemory(false) ;
label3.Text = "GC.GetTotalMemory(true): " + GC.GetTotalMemory(true) ;
Process proc = Process.GetCurrentProcess();
label4.Text = "proc.PrivateMemorySize64: " + proc.PrivateMemorySize64 ;
label5.Text = "proc.WorkingSet64: " + proc.WorkingSet64 ;
proc.Dispose();
}
As might be evident, I tried with and without the Refresh()
command to no avail.
EDIT: The bounty's expired, but if the community would like to award it to someone, then I choose Raful Chizkiyahu.