How to create swap memory on ubuntu and boost server performance?

Running multiple process and specially memory hungry resources can be challenging with limited Memory capacity. You can take less than 2 minute to add additional cushion to those process.

What is swap?

Swap is area of your system hard drive which can be used by system as working memory, specially when there is shortage of RAM for processes.

Having swap can be life saving for the processes in simple web servers, processing large files and so many instance.

Yes for sure, information access will be slower in this area but operating system will prefer to store only LRU data here, least recently used data and will keep the main program running in memory and doesn’t kill it due to lack of low working memory.

Enough talk,
let’s get started.

Check current memory and swap status

sudo swapon --show

This will list you list of swap if available currently on your system.

You can verify no swap memory by running free command and see swap usage, should be 0.

free -h

Check available hard drive storage for swap

To check free hard drive space, you can run following command and see if enough space is available for swap usage.

df -h

Creating swap file

Now since we are aware of available space in our system, you can now choose size of the swap. Usually 1x or 2x size of your RAM is ideal for swap memory.

to create swap first we need to allocate memory and create file for specified size

sudo fallocate -l 2G /swapfile

This will quickly create 2G of file which we will turn into swap.

we can verify correct file size by running command

ls -lh /swapfile


-rw-r--r-- 1 root root 2.0G Mar 11 11:14 /swapfile

Enable swap file

Before enabling swap on this file, we need to lock this file so that no other user than root can access/modify this.

sudo chmod 600 /swapfile

Verify permission using

ls -lh /swapfile


-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile

Now we can mark this file as swap using following

sudo mkswap /swapfile


Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)no label, UUID=95040bf9-5847-4c5c-acee-d1258b272e61

After making swap, lets enable swaping on this file.

sudo swapon /swapfile

Verify the same using

sudo swapon --show


NAME      TYPE SIZE USED PRIO/swapfile file   2G   0B   -1

Now check and verify from free command

free -h


total        used        free      shared  buff/cache   available          990M        356M        207M         52M        425M        397MSwap:          2.0G          0B        2.0G

Make swap permanent

Thats it for the session, but this will not be permanent swap, once we reboot it, this storage will get deleted.

To make swap memory permanent, we need to update /etc/fstab file.

Before updating, first take backup of existing file.

sudo cp /etc/fstab /etc/fstab.bak

Add the swap file information to the end of your /etc/fstab file by typing:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Wow, you have done it.

You can stop here for most of the cases and your swap is ready to be used by your operating system.

Adjusting Swappiness settings

Swappiness settings means the configuration which will allow kernel to decide when to save file to swap. this value varies from 0–100.

Where 0 means kernel will not store anything in swap unless absolute necessary, and 100 means kernel can use it very often.

Based on your requirement, you can configure this value.

To check your current value, type

cat /proc/sys/vm/swappiness



You can adjust this setting using

sudo sysctl vm.swappiness=10

This setting will stay until next reboot, to make changes permanent, we will save this value to file.

Edit following file.

sudo nano /etc/sysctl.conf

At the bottom of this, add


Save and close the file.

You are done.

If you find this quick and useful, make sure to share, bookmark and tweet with your fellow developers so they can quickly go through it and make their life awesome. Thanks for stopping till here. Happy coding.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store