Process.PrivateMemorySize64 returns memory, not private

I am writing a program in C # using .NET 4.5 that will allow me to track the memory, cpu, and network usage of a particular process, and then fetch data according to my needs.

To get the memory usage for a specific process, I check the property PrivateMemorySize64

for that object Process

. I expect to see the private memory used by this process, but instead it displays the amount in Commit, which is confirmed by the Windows Resource Monitor.

My questions:

1) Does anyone know why this error is happening? 2) Is there a fix for it? 3) If there is no fix, is there another easy way to get the private memory reserved for the process?

Here are the relevant parts of my code:

using System;

// I add all the open Processes to an array
Process[] localAll = Process.GetProcesses();

// I then add all the processes to a combobox to select from
// There a button that updates labels with requested info

Process[] p = Process.GetProcessesByName(comboBox1.SelectedItem.ToString());
label1.Text = p[0].PrivateMemorySize64.ToString() + " bytes";

      

+2


source to share


1 answer


From your comment, you said that you are looking for a private working set. This link appears How to calculate a private working set (memory)? that it really isn't part of the Process class. You should use a performance counter instead.

Copied and pasted from another answer in case it gets deleted for some reason.



using System;
using System.Diagnostics;

class Program {
    static void Main(string[] args) {
        string prcName = Process.GetCurrentProcess().ProcessName;
        var counter = new PerformanceCounter("Process", "Working Set - Private", prcName);
        Console.WriteLine("{0}K", counter.RawValue / 1024);
        Console.ReadLine();
    }
}

      

0


source







All Articles