Tracking CPU and Memory usage per process

WindowsSysadminProcess Management

Windows Problem Overview


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.

Windows Solutions


Solution 1 - Windows

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.

Solution 2 - Windows

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

Solution 3 - Windows

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

Solution 4 - Windows

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

@echo off
: Rich Kreider <rjk@techish.net>
: 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.

Solution 5 - Windows

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.

Solution 6 - Windows

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.

Solution 7 - Windows

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

Solution 8 - Windows

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.

Solution 9 - Windows

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

Solution 10 - Windows

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
  }

Solution 11 - Windows

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.

Solution 12 - Windows

Perfmon.exe is built into windows.

Solution 13 - Windows

You might want to have a look at Process Lasso.

Solution 14 - Windows

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.

Solution 15 - Windows

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.

Solution 16 - Windows

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEli BenderskyView Question on Stackoverflow
Solution 1 - WindowsMartin08View Answer on Stackoverflow
Solution 2 - WindowsNiallView Answer on Stackoverflow
Solution 3 - WindowsCppNoobView Answer on Stackoverflow
Solution 4 - WindowsRich KreiderView Answer on Stackoverflow
Solution 5 - WindowsAdy RomantikaView Answer on Stackoverflow
Solution 6 - WindowsdoflynnView Answer on Stackoverflow
Solution 7 - WindowsdyastaView Answer on Stackoverflow
Solution 8 - WindowsseanView Answer on Stackoverflow
Solution 9 - WindowsKaarthikeyanView Answer on Stackoverflow
Solution 10 - WindowsProsenjit SenView Answer on Stackoverflow
Solution 11 - WindowsEli BenderskyView Answer on Stackoverflow
Solution 12 - WindowsSaaS DeveloperView Answer on Stackoverflow
Solution 13 - WindowsmaksaView Answer on Stackoverflow
Solution 14 - WindowsTerrikView Answer on Stackoverflow
Solution 15 - WindowsBryceView Answer on Stackoverflow
Solution 16 - Windowssaeed eivaziView Answer on Stackoverflow