Create and Test Swapfile Linux

Check Swap Space Usage on Linux Server

vmstat -s

Look at the used “used swap” metric.

Check if you have a swapfile:

cat /proc/swaps

If you are not using swap space then these commands should show no results.

Set up a Swap File on your Linux Server

If you have less than 2GB of memory then allocate twice the memory amount to the swap file.

cd /var

touch swapfile

chmod 600 swapfile

ls -la swapfile

Use “dd” command to fill the swap file with 1 GB size (as an example) as follows:

dd if=/dev/zero of=/var/swapfile bs=1024k count=1000

Set up the swap file

mkswap /var/swapfile

Enable the Swap File:

swapon /var/swapfile

check if it has been successfully created:

cat /proc/swaps

swapon -show

Edit /etc/fstabs to ensure the swap file is enabled each time you boot.

Add the following line to this file:

/var/swapfile none swap sw 00

Test Linux Memory Usage and that the Swap File is Working

Use the program stress, installable from the repositories with

sudo apt-get install stress

To test your RAM use stress -m x where x is the number of workers which will fill up the ram. Choose more workers to use more RAM.


stress -m 5

then look at memory usage with

vmstat -s


free -m