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.

Dheeraj jha
Intelligent computing
3 min readMar 11, 2021

--

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

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.

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.

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

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

we can verify correct file size by running command

Output

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.

Verify permission using

Output

Now we can mark this file as swap using following

Output

After making swap, lets enable swaping on this file.

Verify the same using

output

Now check and verify from free command

Output

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.

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

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

output

You can adjust this setting using

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

Edit following file.

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.

--

--

Dheeraj jha
Intelligent computing

DevOps Engineer | Team lead | AWS | Docker | CI/CD | Gitlab-CI