Ubuntu – How to see what process spiked CPU usage and froze the system a few seconds ago

cpu load

My Ubuntu 18.04 randomly freezes for a few seconds. I can move the mouse cursor (sometimes), but otherwise the OS is unresponsive and I can't switch to any other application.

When it resumes, I can go to the System Monitor and see a spike in CPU usage a few seconds ago (only to 70% though), but that doesn't tell me WHAT used the CPU then.

How can you find out what processed chewed up the CPU very recently? (I don't think this is due to disk I/O).

In the meantime I've determined that the culprit is WebStorm, a Java-based IDE, by isolating suspected apps. While using VS Code, there were no freezes.

Below is some additional diagnostic info asked for in the comments.

$ free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        8.6G        2.3G        2.0G        4.5G        4.4G
Swap:           15G        487M         15G
$ sysctl vm.swappiness
vm.swappiness = 10

When WebStorm freezes, the load in System Monitor does show a spike, but nowhere near 100%.

Best Answer

To get past history of CPU utilization is not possible unless one is monitoring the system with a tool that captures CPU activity. One method is to run cpustat and capture the output and see where the CPU utilization is occurring, for example:

sudo apt-get install cpustat
cpustat -xS | tee cpu.log

and when you get a slowdown one can view cpu.log and see what was busy running.