Let’s first talk about the reason I started looking for this. I have a couple of services running in Ubuntu including DBs like MySQL, MongoDB, etc. along with running nGinx and other services.
However, sometimes, I noticed that the memory consumption goes upwards and it’s wise to know which process could be responsible for this.
I decided to look into this using the ps command
ps -eo pmem,pcpu,pid,args | tail -n +2 | sort -rnk 1 | head
Let’s look at the arguments provided:
ps | Current process snapshot report |
-e | Select all processes. Identical to -A. |
-o | format is a single argument in the form of a blank-separated or comma-separated list, which offers a way to specify individual output columns. |
pmem | the ratio of the process’s resident set size to the physical memory on the machine, expressed as a percentage. |
pcpu | CPU utilization of the process in the “##.#” format. Currently, it is the CPU time used divided by the time the process has been running (cputime/real time ratio), expressed as a percentage. |
pid | A number representing the process ID |
args | Command with all its arguments as a string. |
tail -n +2 | Output lines starting to the second line |
sort -rnk 1 | r (reverse) n(numeric sort) by column 1 i.e., pmem |
head | Output the first 10 lines |
Hope it helps!