[windows] Tracking CPU and Memory usage per process

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.

This question is related to windows sysadmin process-management

The answer is


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.


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.


Perfmon.exe is built into windows.


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


On macOS, I use Instrument and attach the process into it.

enter image description here

enter image description here


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
  }

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.


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.


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.


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.


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


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.


Perfmon.exe is built into windows.


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


On macOS, I use Instrument and attach the process into it.

enter image description here

enter image description here


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


You might want to have a look at Process Lasso.


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.


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


You might want to have a look at Process Lasso.


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


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.


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.


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.


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.


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
  }

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.


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


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


You might want to have a look at Process Lasso.


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


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.


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.


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.

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.


Perfmon.exe is built into windows.


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


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


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.

Perfmon.exe is built into 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.


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


You might want to have a look at Process Lasso.


Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to sysadmin

Locate the nginx.conf file my nginx is actually using Crontab Day of the Week syntax Calling JMX MBean method from a shell script Tar error: Unexpected EOF in archive Opening a remote machine's Windows C drive Best practice to run Linux service as a different user How to find out what group a given user has? Comprehensive methods of viewing memory usage on Solaris How to use SSH to run a local shell script on a remote machine? How can I delete a service in Windows?

Examples related to process-management

How to find the process id of a running Java process on Windows? And how to kill the process alone? GIT vs. Perforce- Two VCS will enter... one will leave Tracking CPU and Memory usage per process