1

I’m trying to measure the RAM usage of a specific application using C# (.NET Framework 4.6.2).

I’ve already done some research and managed to get a result using Process.WorkingSet64,
but the value doesn’t match what I see in Task Manager.

What’s the correct way to get the same value that Task Manager shows in the "Memory" column?

I want to get same value that Task Manager shows

3
  • 2
    Why? What is the actual goal? What kind of problem are you trying to solve? There are several measures for "memory usage", and the GC tend to make things more complicated. Commented Oct 6 at 12:52
  • Unclear to me: Do you want to measure a different App or the App the measuring code is executing in? In other words: App A measures App A or App A measures App B ? Commented Oct 6 at 12:56
  • 1
    Also, have you heard of performance counters ? Commented Oct 6 at 12:58

1 Answer 1

1
class Program
{
    static void Main()
    {
        string processName = "notepad"; 

        try
        {            
            var counter = new PerformanceCounter("Process", "Working Set - Private", processName);
            float memoryBytes = counter.NextValue();

            double memoryMB = memoryBytes / 1024 / 1024;
            
            Console.WriteLine($"[{processName}] Memory (Task Manager 'Memory' column): {memoryMB:F1} MB");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
        Console.WriteLine();
        Console.WriteLine("Press any key to exit...");
        Console.ReadKey(); 
    }
}

The memory value obtained using this code matches the value displayed in Task Manager’s Memory column, as shown in the screenshot below

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.