188

I suspect that one of my applications eats more CPU cycles than I want it to. The problem is - it happens in bursts, and just looking at the task manager doesn't help me as it shows immediate usage only.

Is there a way (on Windows) to track the history of CPU & Memory usage for some process. E.g. I will start tracking "firefox", and after an hour or so will see a graph of its CPU & memory usage during that hour.

I'm looking for either a ready-made tool or a programmatic way to achieve this.

17 Answers 17

185

Press Win+R, type perfmon and press Enter. When the Performance window is open, click on the + sign to add new counters to the graph. The counters are different aspects of how your PC works and are grouped by similarity into groups called "Performance Object".

For your questions, you can choose the "Process", "Memory" and "Processor" performance objects. You then can see these counters in real time

You can also specify the utility to save the performance data for your inspection later. To do this, select "Performance Logs and Alerts" in the left-hand panel. (It's right under the System Monitor console which provides us with the above mentioned counters. If it is not there, click "File" > "Add/remove snap-in", click Add and select "Performance Logs and Alerts" in the list".) From the "Performance Logs and Alerts", create a new monitoring configuration under "Counter Logs". Then you can add the counters, specify the sampling rate, the log format (binary or plain text) and log location.

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

7 Comments

What is the ideal interval to grab a data sample in a counter log?
MS has obviously changed the UI to PerformanceMonitor since you wrote this. Any idea how you do this in Windows 2008 R2?
@MartinBrown - Found this. It's excellent.
anyone knows how to do the second part of saving a log on vista? There doesnt seem to be "Performance Logs and Alerts" anywhere
Right click Data Collector Sets->User Defined. Select New->Data Collector Set. Give it a name and select Create manually. Click Next. Select Performance counter. Click Next. Add the performance counters and enter the sample interval. Then under Data Collector Sets you right click your set and click "Start". After some time you right click your set and click "Stop". Then you can find the report under Reports->User Defined->Your set. Right click the graph and select Save Data As.
|
48

Process Explorer can show total CPU time taken by a process, as well as a history graph per process.

5 Comments

So how would you use it to find a process that randomly starts using more memory or processor power in a short burst. Could you elaborate?
Right click on column headers, choose Select Columns, then check "CPU History" under "Process Performance"
can you reset it to zero?
Can I log into a file the memory consumption of a given PID?
Just wanted to point out for other users: the CPU graph for each process only starts recording after you open it for the first time, even if Process Explorer was open, so this method is virtually useless for capturing a sporadic event and trying to isolate it to a single process. I will point out that if you click the global system CPU graph (which starts recording upon opening Process Explorer) and hover your mouse over any point, it will show the process that used the CPU the most at that point.
26

Using perfmon.exe, I have tried using the "Private Bytes" counter under "Process" counters for tracking memory usage and it works well.

1 Comment

This is the correct answer. You can also get CPU information under Process. Click on Process, then select your app (Firefox in your example) under instances of selected object.
12

maybe you can use this. It should work for you and will report processor time for the specified process.

@echo off
: Rich Kreider <[email protected]>
: report processor time for given process until process exits (could be expanded to use a PID to be more
: precise)
: Depends:  typeperf
: Usage:  foo.cmd <processname>

set process=%~1
echo Press CTRL-C To Stop...
:begin
for /f "tokens=2 delims=," %%c in ('typeperf "\Process(%process%)\%% Processor Time" -si 1 -sc 1 ^| find /V "\\"') do (
if %%~c==-1 (
goto :end
) else (
echo %%~c%%
goto begin
)
)

:end
echo Process seems to have terminated.

Comments

6

I agree, perfmon.exe allows you to add counters (right click on the right panel) for any process you want to monitor.

Performance Object: Process Check "Select instances from list" and select firefox.

2 Comments

Thanks. How do I see the history of cpu usage over a pre-defined time, like an hour ?
When you are measuring % processor time, you are selecting individual cores, not processes.
6

WMI is Windows Management Instrumentation, and it's built into all recent versions of Windows. It allows you to programmatically track things like CPU usage, disk I/O, and memory usage.

Perfmon.exe is a GUI front-end to this interface, and can monitor a process, write information to a log, and allow you to analyze the log after the fact. It's not the world's most elegant program, but it does get the job done.

2 Comments

Technically perfmon is an interface to the underlying Windows Performance Counter API which predates WMI by many years. WMI also exposes the performance counter API within its namespace.
But does perfmon need to be running? Or willthe WMI log the information in the background?
4

Process Lasso is designed more for process automation and priority class optimization, not graphs. That said, it does offer per-process CPU utilization history (drawn as a white line on the graph) but it does NOT offer per-process memory utilization history.

DISCLAIMER: I am the author of Process Lasso, but am not actually endorsing it here - as there are better solutions (perfmon being the best).

The best thing ever is Windows Vista+ Resource and Performance Monitor. It can track usage of CPU, Memory, Network, and Disk accesses by processes over time. It is a great overall system information utility that should have been created long ago. Unless I am mistaken, it can track per-process CPU and memory utilization over time (amongst the other things listed).

1 Comment

Process Lasso is the easy answer now. It is 2025 and perform still sucks and cannot help do something as simple as give a running "CPU avg"--something Process Lasso has with zero config as soon as you launch the app.
2

You can also try using a C#/Perl/Java script get the utilization data using WMI Commands, and below is the steps for it.

We need to execute 2 WMI Select Queries and apply CPU% utilization formula

1. To retrieve the total number of logical process

select NumberOfLogicalProcessors from Win32_ComputerSystem

2. To retrieve the values of PercentProcessorTime, TimeStamp_Sys100NS ( CPU utilization formula has be applied get the actual utilization percentage)and WorkingSetPrivate ( RAM ) minimum of 2 times with a sleep interval of 1 second

select * from Win32_PerfRawData_PerfProc_Process where IDProcess=1234

3. Apply CPU% utilization formula

CPU%= ((p2-p1)/(t2-t1)*100)/NumberOfLogicalProcessors

p2 indicated PercentProcessorTime retrieved for the second time, and p1 indicateds the PercentProcessorTime retrieved for the first time, t2 and t1 is for TimeStamp_Sys100NS.

A sample Perl code for this can be found in the link http://www.craftedforeveryone.com/cpu-and-ram-utilization-of-an-application-using-perl-via-wmi/

This logic applies for all programming language which supports WMI queries

1 Comment

WMI and Powershell ?
2

Although I have not tried this out, ProcDump seems like a better solution.

Description from site:

ProcDump is a command-line utility whose primary purpose is monitoring an application for CPU spikes and generating crash dumps during a spike that an administrator or developer can use to determine the cause of the spike. ProcDump also includes hung window monitoring (using the same definition of a window hang that Windows and Task Manager use), unhandled exception monitoring and can generate dumps based on the values of system performance counters. It also can serve as a general process dump utility that you can embed in other scripts.

Comments

2

There was a requirement to get status and cpu / memory usage of some specific windows servers. I used below script:

This is an example of Windows Search Service.

  $cpu = Get-WmiObject win32_processor
  $search = get-service "WSearch"
  if ($search.Status -eq 'Running')
  {
  $searchmem = Get-WmiObject Win32_Service -Filter "Name = 'WSearch'"
  $searchid = $searchmem.ProcessID
  $searchcpu1 = Get-WmiObject Win32_PerfRawData_PerfProc_Process | Where {$_.IDProcess -eq $searchid}
  Start-Sleep -Seconds 1
  $searchcpu2 = Get-WmiObject Win32_PerfRawData_PerfProc_Process | Where {$_.IDProcess -eq $searchid}
  $searchp2p1 = $searchcpu2.PercentProcessorTime - $searchcpu1.PercentProcessorTime
  $searcht2t1 = $searchcpu2.Timestamp_Sys100NS - $searchcpu1.Timestamp_Sys100NS
  $searchcpu = [Math]::Round(($searchp2p1 / $searcht2t1 * 100) /$cpu.NumberOfLogicalProcessors, 1)
  $searchmem = [Math]::Round($searchcpu1.WorkingSetPrivate / 1mb,1)
  Write-Host 'Service is' $search.Status', Memory consumed: '$searchmem' MB, CPU Usage: '$searchcpu' %'
  }

  else
  {
  Write-Host Service is $search.Status -BackgroundColor Red
  }

Comments

1

Hmm, I see that Process Explorer can do it, although its graphs are not too convenient. Still looking for alternative / better ways to do it.

Comments

1

Perfmon.exe is built into windows.

Comments

1

You might want to have a look at Process Lasso.

Comments

0

I use taskinfo for history graph of CPU/RAM/IO speed. http://www.iarsn.com/taskinfo.html

But bursts of unresponsiveness, sounds more like interrupt time due to a falty HD/SS drive.

Comments

0

Under Windows 10, the Task Manager can show you cumulative CPU hours. Just head to the "App history" tab and "Delete usage history". Now leave things running for an hour or two:

Windows 10 Cumulative CPU time

What this does NOT do is break down usage in browsers by tab. Quite often inactive tabs will do a tremendous amount of work, with each open tab using energy and slowing your PC.

Comments

0

Download process monitor

  1. Start Process Monitor

  2. Set a filter if required

  3. Enter menu Options > Profiling Events

  4. Click "Generate thread prof‌iling events", choose the frequency, and click OK.

  5. To see the collected historical data at any time, enter menu Tools > Process Activity Summary

Comments

0

In C# ,

var query = (from p in System.Diagnostics.Process.GetProcesses() orderby p.PrivateMemorySize64 descending select p) .Skip(0) .Take(10) .ToList();

            foreach (var item in query)
            {
                string ProcessName = item.ProcessName + "-" + item.Id;
                long Size = item.PrivateMemorySize64;
            }

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.