Show What’s on Swap in Linux

If you find your machine running sluggish, it might be the case that you have too many things running and it’s hitting the swap. You can check how bad (or good) things are with htop, but it won’t tell you which process is sitting on swap. To find out, I’ve cooked up a tiny script:

It will look in /proc/[PID]/status and get the VmSwap field. Then, it’ll print it out along with the PID and the command line (from ps -ax).

Note: This is a rather crude script and it’ll give some errors for processes which have finished between running the initial ps -ax in the loop and the other ones.

A one-liner variant is:

…which you can then pipe to e.g. a file or another command for further processing. One example is:

Which gives me the processes occupying the most swap space first.

HTH,


A little experiment: If you find this post and ad below useful, please check the ad out :-)




Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top