Add Swap file
First we check out the memory usage:
[root@srv-2 /root]# free -m total used free shared buffers cached Mem: 251 242 8 22 11 32 -/+ buffers/cache: 198 52 Swap: 133 133 0 |
Make sure we have 128 MB laying around somewhere:
[root@srv-2 /root]# df Filesystem 1k-blocks Used Available Use% Mounted on /dev/hda9 132207 33429 91952 27% / /dev/hda1 15522 2537 12184 17% /boot /dev/hda6 6143236 739000 5092176 13% /opt /dev/hda7 1035660 836204 146848 85% /usr /dev/hda5 2071384 344048 1622112 17% /usr/local /dev/hda8 303344 14439 273244 5% /var |
OK, we’re going to make a swap file in /opt by using dd to create a file 128 MB in size.
[root@srv-2 /opt]# dd if=/dev/zero of=swapfile bs=1024 count=132207 132207+0 records in 132207+0 records out [root@srv-2 /opt]# ls -l total 132364 drwxr-xr-x 20 usr-3 users 4096 May 22 10:46 usr-3 drwxr-xr-x 2 root root 16384 Feb 21 07:04 lost+found -rw-r--r-- 1 root root 135379968 May 29 11:52 swapfile |
Hey, I know, let’s not make it world-readable…
[root@srv-2 /opt]# chmod 600 swapfile [root@srv-2 /opt]# ls -l total 132364 drwxr-xr-x 20 usr-3 users 4096 May 22 10:46 usr-3 drwxr-xr-x 2 root root 16384 Feb 21 07:04 lost+found -rw------- 1 root root 135379968 May 29 11:52 swapfile |
Now we set up the swap area and enable it.
[root@srv-2 /opt]# mkswap swapfile Setting up swapspace version 1, size = 135372800 bytes [root@srv-2 /opt]# swapon swapfile |
And viola! Twice as much swap as before.
[root@srv-2 /opt]# free total used free shared buffers cached Mem: 257632 254632 3000 2512 36172 15096 -/+ buffers/cache: 203364 54268 Swap: 268708 136512 132196 |
You can edit /etc/fstab to enable your swap file automatically at boot time.
By adding an entry like this:
/opt/swapfile swap swap defaults 0 0 |
Sure, swapping’s ugly, slow and will grind your hard drives to dust. But even modern systems which have been tuned for performance require a generous oodle of swap space.
How much swap space is currently used by the system?
Free command displays the swap space. free -k shows the output in KB.
# free -k total used free shared buffers cached Mem: 3082356 2043700 1038656 0 50976 1646268 -/+ buffers/cache: 346456 2735900 Swap: 4192956 0 4192956
Swapon command with option -s, displays the current swap space in KB.
# swapon -s Filename Type Size Used Priority /dev/sda2 partition 4192956 0 -1
Swapon -s, is same as the following.
# cat /proc/swaps Filename Type Size Used Priority /dev/sda2 partition 4192956 0 -1
How to find out which process is using swap space?
We can also use bash script to pick up the process from /proc file system. So, use the following script :
—–
#!/bin/bash
# Get current swap usage for all running processes
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d | egrep “^/proc/[0-9]”` ; do
PID=`echo $DIR | cut -d / -f 3`
PROGNAME=`ps -p $PID -o comm –no-headers`
for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk ‘{ print $2 }’`
do
let SUM=$SUM+$SWAP
done
echo “PID=$PID – Swap used: $SUM – ($PROGNAME )”
let OVERALL=$OVERALL+$SUM
SUM=0
done
echo “Overall swap used: $OVERALL”
—–
Save it as getswapusage.sh and change its permission like :
#chmod 755 getswapusage.sh
Now run it like :
#./getswapusage.sh |sort -n -k 5
We can view swap usage at that particular moment by particular process.
We can also monitor total swap usage by following command :
#watch cat /proc/meminfo