Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contradictory reporting of total Process memory usage in C# Winforms app

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:

pic

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.

like image 704
Dan W Avatar asked May 25 '20 20:05

Dan W


People also ask

How to print memory usage in c#?

You can use: Process proc = Process. GetCurrentProcess();

How does PS report memory usage?

Calculated as the sum of the number of working segment and code segment pages in memory times 4 (that is, the RSS value), divided by the size of the real memory in use, in the machine in KB, times 100, rounded to the nearest full percentage point.

Which profiling will analyze the memory usage of the application?

When the Diagnostic Tools window appears, choose the Memory Usage tab, and then choose Heap Profiling.


3 Answers

The windows task manager is showing only the memory that actually on the RAM and the process memory is including the pages (on the disk ) that the reason the process memory can be bigger from the actual RAM on your machine

like image 56
Raful Chizkiyahu Avatar answered Sep 30 '22 11:09

Raful Chizkiyahu


Task Manager Doesn't give you the "Exact" RAM usage. Try using Visual Studio's Diagnostic Tools or resourcemonitor

However, for getting the exact memory usage,I think this code will work

using System.Diagnostics;
// This obtains the current application process
Process thisProcess = Process.GetCurrentProcess();

// This obtains the memory used by the process
long usedMemory = thisProcess.PrivateMemorySize64;

// This will display the memory used by your C# app
 label6.Text= System.Convert.ToString(usedMemory)

I've assumed that your next label will be "label6". Change it if you've added more labels.

Hope it helps :)

EDIT: If you divide the "usedMemory" by 2850023.23, You can get a value which is approximately equal to the memory shown in the Task Manager.

// This obtains the current application process
        Process thisProcess = Process.GetCurrentProcess();

        // This obtains the memory used by the process
        long usedMemory = thisProcess.PrivateMemorySize64;

        // 3. This will display the memory used by your C# app
        label6.Text = System.Convert.ToString(usedMemory/ 2850023.23);
like image 22
Vinayak9769 Avatar answered Sep 30 '22 10:09

Vinayak9769


i've noticed that Visual studio debugging tools report higher memory usage than task manager in most cases while the task manager only reports the usage of RAM the debugging tools in VS will take into account everything like pagefiles...etc

i think they would be more reliable

like image 30
Massaynus Avatar answered Sep 30 '22 10:09

Massaynus