Linux: a quick way to find swap usage of processes

Recently i had to deal with a swap usage issue, and i need a fast way to find out top swap consumer processes, a simple one-liner is this

Recently i had to deal with a swap usage issue, and i need a fast way to find out top swap consumer processes, a simple one-liner is thisfor file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -rrsyslogd 3928992 kB
lvmetad 2604 kB

If you face often swap issues and you want to track the swap usage of a process you can do the following enhancement, the bellow one-liner will log on each run the swap usage per process with a timestamp to help you know when swap started growingfor file in /proc/*/status ; do echo -n `date` ;awk '/VmSwap|Name/{printf ";"$2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r >> swap_logging.txt

The contents of the file will be something like thisMon Oct 11 22:07:22 EEST 2021;wslbridge2-back ;0 kB
Mon Oct 11 22:07:22 EEST 2021;sort ;0 kB
Mon Oct 11 22:07:22 EEST 2021;init ;0 kB
Mon Oct 11 22:07:22 EEST 2021;init ;2 kB
Mon Oct 11 22:07:22 EEST 2021;init ;2 kB
Mon Oct 11 22:07:22 EEST 2021;init ;0 kB
Mon Oct 11 22:07:22 EEST 2021;init ;0 kB
Mon Oct 11 22:07:22 EEST 2021;bash ;0 kB
Mon Oct 11 22:07:22 EEST 2021;bash ;0 kB
Mon Oct 11 22:07:27 EEST 2021;wslbridge2-back ;0 kB
Mon Oct 11 22:07:27 EEST 2021;sort ;0 kB
Mon Oct 11 22:07:27 EEST 2021;init ;0 kB
Mon Oct 11 22:07:27 EEST 2021;init ;0 kB
Mon Oct 11 22:07:27 EEST 2021;init ;3 kB
Mon Oct 11 22:07:27 EEST 2021;init ;3 kB
Mon Oct 11 22:07:27 EEST 2021;init ;0 kB
Mon Oct 11 22:07:27 EEST 2021;bash ;0 kB
Mon Oct 11 22:07:27 EEST 2021;bash ;0 kB
Mon Oct 11 22:07:27 EEST 2021;awk ;0 kB
Mon Oct 11 22:07:27 EEST 2021;awk ;0 kB
Mon Oct 11 22:07:33 EEST 2021;wslbridge2-back ;0 kB
Mon Oct 11 22:07:33 EEST 2021;sort ;0 kB
Mon Oct 11 22:07:33 EEST 2021;init ;0 kB
Mon Oct 11 22:07:33 EEST 2021;init ;0 kB
Mon Oct 11 22:07:33 EEST 2021;init ;0 kB
Mon Oct 11 22:07:33 EEST 2021;init ;4 kB
Mon Oct 11 22:07:33 EEST 2021;init ;4 kB
Mon Oct 11 22:07:33 EEST 2021;bash ;0 kB
Mon Oct 11 22:07:33 EEST 2021;bash ;0 kB
Mon Oct 11 22:07:33 EEST 2021;awk ;0 kB
Mon Oct 11 22:07:33 EEST 2021;awk ;0 kB
Mon Oct 11 22:07:40 EEST 2021;wslbridge2-back ;0 kB
Mon Oct 11 22:07:40 EEST 2021;sort ;0 kB
Mon Oct 11 22:07:40 EEST 2021;init ;0 kB
Mon Oct 11 22:07:40 EEST 2021;init ;5 kB
Mon Oct 11 22:07:40 EEST 2021;init ;5 kB
Mon Oct 11 22:07:40 EEST 2021;init ;0 kB
Mon Oct 11 22:07:40 EEST 2021;init ;0 kB
Mon Oct 11 22:07:40 EEST 2021;bash ;0 kB
Mon Oct 11 22:07:40 EEST 2021;bash ;0 kB
Mon Oct 11 22:07:40 EEST 2021;awk ;0 kB
Mon Oct 11 22:07:40 EEST 2021;awk ;0 kB

A quick way to find the timestamp that the swap started growing is this one-liner, It will sum the swap usage per timestamp and sort itcat swap_logging.txt  | cut -d ";" -f 1,3 | awk -F ";" 'NR == 1 { next }{ a[$1] += $2 }END {for (i in a) {print i, a[i] | "sort -rnk2";}}'Mon Oct 11 22:07:40 EEST 2021 10
Mon Oct 11 22:07:33 EEST 2021 8
Mon Oct 11 22:07:27 EEST 2021 6
Mon Oct 11 22:07:22 EEST 2021 4

I hope you find this article useful :)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…