Find Processes Eating Your SWAP Memory on Linux and Build Alert Script

What is Eating my SWAP Memory

Finding Processes Eating SWAP Create Monitoring Script

In this article i will show you how to find the SWAP memory usage and create a script to monitor it and send an email whenever it's lower than certain size.

Find SWAP used and free space

free -m

Or

cat /proc/meminfo | grep -i swap

vmstat

Find out processes

You can use any of below methods

  1. Using 'Top' command then press on 'O' , 'p' and 'Enter'
  2. Use smem
    1. wget https://www.selenic.com/smem/download/smem-1.4.tar.gz --no-check-certificate
    2. tar xfvz smem-1.4.tar.gz
    3. cd smem-1.4
    4. ./smem -s swap -t -k -n

 

High SWAP Memory Cause

This can happen for several reasons and depends on what you will find by running above commands, for example if HTTPD is the main process eating your memory then you can restart it as a temporary solution and then investigate the root cause.

 

SWAP Monitoring Script

Below script will check the free SWAP memory and send an email whenever it's below 8000, replace mail@mail.com with 'to emails' and from ones.

 

#! /bin/bash

swpSize=$(free -m | grep -i swap | awk '{print $4}') ;

if [ $swpSize -lt 8000 ]
then

echo "$(hostname) SWAP is low" |mailx -s "$(hostname) SWAP is low" mail@mail.com -- -f mail@mail.com

else
exit 0
fi

 

 


Subscribe to
for video tutorials updates

One thought on “Find Processes Eating Your SWAP Memory on Linux and Build Alert Script

  1. Pingback: Why Multiple HTTPD in the Process

Comments are closed.