简体   繁体   中英

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:

图片

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.

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

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);

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

You need better tooling, TaskManager will only give you approximate memory usage at a certain moment.

If you suspect a memory leak, the tried and true method is to take memory dumps(snapshots) of the application.

A snapshot should be taken at application startup, then at subsequent intervals. If memory rises slowly you will have to wait a bit between each snapshot. You can then compare the snapshots. This method allows you to analyze and compare object allocations, object counts, even unmanaged memory.

Microsoft has written a guide for the Memory Usage tool integrated into Visual Studio . In most cases this should suffice in identifying the leak.

If you find the integrated tooling limited, have a look at this SO question and others linked in the sidebar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM