You are here

how to add a swap file

lets assume, your server is setup in the cloud. and in the cloud RAM costs quite a bit. but the minimum configuration server provided has a lot of disk space. ssd disk space. an ssd disk is pretty fast. and you dont need all that ssd disk space for data. and you would like to use that fast disk space to expand your swap memory.

well read on!

create the file

dd if=/dev/zero of=/root/swap.file bs=1M count=2048
chmod 600 /root/swap.file

modify /etc/fstab

echo "/root/swap.file               swap                    swap    defaults        0 0" >> /etc/fstab

enable swap

mkswap /root/swap.file 
swapon /root/swap.file
swapon -s #verify it is listed

lets view the current swap before enabling the new swap file

free -m

then disable ALL swap memory on the system

swapoff -a

view it again if you want

free -m

and now enable ALL swap memory on the system that is recorded in the /etc/fstab file

swapon -a 

verify swap memory increased

and finally, verify the swap memory has increased by comparing the results of free

free -m

or to visually see the difference in one line

free -m; swapoff -a;free -m ; swapon -a; free -m