List Processes by Memory Usage in Linux
Posted on May 9, 2019  (Last modified on November 9, 2022 )
2 minutes • 236 words
I recently was running into an issue where a Systemd service was crashing due to an out of memory error. I was pretty sure that my project wasn’t taking up too much memory, but I wasn’t certain what was. Thankfully, with the use of some command-line wizardry, I was able to find out that Hyper (the terminal app) was using almost 3gb of memory!
ps -e -o pid,vsz,comm= | sort -r -n -k 2
We have two commands here, each with their own
man page should you want to learn more. The first command is
ps which displays information about all of your running processes.
-e flag is actually identical to the
-A flag, which tells the command to list all users processes, including those not controlling terminals.
-o flag takes parameters (that’s the
pid,vsz,comm=) and that controls how the output is formatted. In our case, that will format the output with a process id, virtual size in kilobytes, and the command.
We then pipe this into another command named
-r reverses the sort
order, so the highest value will be at the top in our case.
-n indicates that
we’d like to sort our fields numerically by arithmetic value and finally
takes a parameter indicating the field to key your sort off of. In our case,
it’s our virtual size in kilobytes, indicated by the “2”nd field in our output.