llk
llk

Reputation: 2561

Having issues listing process memory and CPU usage using Process Class c#

I am having issues listing the memory and CPU usage of processes in C#. I am getting a runtime error "Access denied" when I try to get TotalProcessorTime.

Also, when I use PeakWorkingSet64, I am getting unreasonable numbers. For example, I know that steam.exe is not taking up 135380992 KB.

Is there a better method of obtaining memory usage? My goal is to display it like task manager like 1024K.

As for the CPU usage, I tried running my compiler (VS2010 Professional) under administrator privileges and I am on an administrator account but I got the same error. Also I am running windows 32 bit if that has anything to do with the matter. Thanks!

var processes = Process.GetProcesses();
listBox1.Items.Clear();
foreach (var process in processes)
{                
    listBox2.Items.Add(process.PeakWorkingSet64 + process.ProcessName + "");
    listBox3.Items.Add(process.TotalProcessorTime + "");
}

Upvotes: 3

Views: 3373

Answers (3)

Pradeep Kumar Das
Pradeep Kumar Das

Reputation: 891

Run the application as as administrator mode so it will not say anything about Access denied message.

if you are using visual studio then open visual studio as administrator mode . or run the program in administrator mode .

Upvotes: 0

Chris Taylor
Chris Taylor

Reputation: 53709

Process.PeakWorkingSet64 returns the number of bytes not KB, so that would probably be 132203KB or 129MB does that sound more reasonable?

Have you tried running the application outside of VS, running it as Administrator? It might be that the VS hosting process is not running as under the elevated privileges and therefore preventing you from calling TotalProcessorTime, I have not tested this so I might be off track here.

Update: I just did a quick test and it seems that the TotalProcessorTime property actually attempts to open a handle to the process, which even as Administrator you might not have sufficient privileges to do for certain processes. I would suggest that you look at using the windows PerformanceCounter to get the information you are looking for.

Upvotes: 4

Mike Marshall
Mike Marshall

Reputation: 7850

Most readings are usually in bytes, not KB. Also, working directly with the Process class is tough due to issues with permissions and privileges. Try reading performance counter data instead. Here is a sample function that dumps various process memory usage stats for all processes (it uses log4net for logging):

public static void LogProcessMemoryUsage()
{
    try
    {
        PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");

        string[] instanceNames = cat.GetInstanceNames();

        foreach (string name in instanceNames)
        {
            try
            {
                PerformanceCounter counter = new PerformanceCounter("Process", "Private Bytes", name, true);
                PerformanceCounter setCounter = new PerformanceCounter("Process", "Working Set", name, true);
                PerformanceCounter poolNPCounter = new PerformanceCounter("Process", "Pool Nonpaged Bytes", name, true);

                log.InfoFormat("Memory usage for process [{0}]", name);
                log.InfoFormat("\tMem Usage:       {0} KB", (setCounter.NextValue()/1024f));
                log.InfoFormat("\tVM Size:         {0} KB", (counter.NextValue()/1024f));
                log.InfoFormat("\tNon-Paged Pool:  {0} KB", (poolNPCounter.NextValue() / 1024f));
            }
            catch (Exception ex)
            {
                log.InfoFormat("Could not read memory stats for process {0}", name);
            }
        }
    }
    catch(Exception ex2)
    {
        log.Info("Cannot retrieve memory performance counter statistics");
    }
}

Upvotes: 4

Related Questions