How to add swap space on Ubuntu

Ajith Kumar
Analytics Vidhya
Published in
5 min readApr 25, 2021

--

The smartest way of shielding against out-of-memory errors in your applications is to add some swap space to your server. In this guide, we will cover how to add a swap file to an Ubuntu 18.04 server.

What is Swap?

Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM. Basically, this gives you the ability to increase the amount of information that your server can keep in its working “memory”, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.

Checking the System for Swap Information

Before we begin, we need to check if the system already has some swap space available. It is possible to have multiple swap files or swap partitions, but generally one should be enough though.

We can see if the system has any configured swap by typing:

sudo swapon — show

If you don’t get back any output, this means your system does not have swap space available currently.

You can verify that there is no active swap using the free utility:

free -h

Outputtotal        used        free      shared  buff/cache   available
Mem: 985M 84M 222M 680K 678M 721M
Swap: 0B 0B 0B

As you can see in the Swap row of the output, no swap is active on the system.

Checking Available Space on the Hard Drive Partition

Before we create our swap file, we’ll check our current disk usage to make sure we have enough space. Do this by entering:

df -h

OutputFilesystem      Size  Used Avail Use% Mounted on
udev 481M 0 481M 0% /dev
tmpfs 99M 656K 98M 1% /run
/dev/vda1 25G 1.4G 23G 6% /
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/vda15 105M 3.4M 102M 4% /boot/efi
tmpfs 99M 0 99M 0% /run/user/1000

The device with / in the Mounted on column is our disk in this case. We have plenty of space available in this example (only 1.4G used). Your usage will probably be different.

Generally for swap size, an amount equal to or double the amount of RAM on your system is a good starting point. Another good rule of thumb is that anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.

Creating a Swap File

Let’s create it now

Now that we know our available hard drive space, we can create a swap file on our filesystem. We will allocate a file of the swap size that we want called swapfile in our root (/) directory.

The best way of creating a swap file is with the fallocate program. This command instantly creates a file of the specified size.

Since the server in our example has 1G of RAM, we will create a 1G file in this guide. Adjust this to meet the needs of your own server:

sudo fallocate -l 1G /swapfile

We can verify that the correct amount of space was reserved by typing:

ls -lh /swapfile

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

Enabling the Swap File

Now that we have a file of the correct size available, we need to actually turn this into swap space.

First, we need to lock down the permissions of the file so that only the users with root privileges can read the contents. This prevents normal users from being able to access the file.

Make the file only accessible to root by typing:

sudo chmod 600 /swapfile

Verify the permissions change by typing:

ls -lh /swapfile

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

As you can see, only the root user has the read and write flags enabled.

We can now mark the file as swap space by typing:

sudo mkswap /swapfile

OutputSetting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf

After marking the file, we can enable the swap file, allowing our system to start utilising it:

sudo swapon /swapfile

Verify that the swap is available by typing:

sudo swapon — show

OutputNAME      TYPE  SIZE USED PRIO
/swapfile file 1024M 0B -2

We can check the output of the free utility again:

free -h

Outputtotal        used        free      shared  buff/cache   available
Mem: 985M 84M 220M 680K 680M 722M
Swap: 1.0G 0B 1.0G

Congrats!, Our swap has been set up successfully and our operating system will begin to use it as necessary.

Making the Swap File Permanent

Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically.

We can change this by adding the swap file to our /etc/fstab file.

Back up the /etc/fstab file in case anything goes wrong:

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

Next we’ll review some settings we can update to tune our swap space.

Tuning your Swap Settings

Adjusting the Swappiness Property

The swappiness parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage.

We can see the current swappiness value by typing:

cat /proc/sys/vm/swappiness

Output60

For a Desktop, a swappiness setting of 60 is not a bad value. For a server, you might want to move it closer to 0.

We can set the swappiness to a different value by using the sysctl command.

For instance, to set the swappiness to 10, we could type:

sudo sysctl vm.swappiness=10

Outputvm.swappiness = 10

This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

At the bottom, you can add:

/etc/sysctl.conf

vm.swappiness=10

Save and close the file when you are finished.

Adjusting the Cache Pressure Setting

Another related value that you might want to modify is the vfs_cache_pressure. This setting configures how much the system will choose to cache inode and dentry information over other data.

cat /proc/sys/vm/vfs_cache_pressure

Output100

As it is currently configured, our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:

sudo sysctl vm.vfs_cache_pressure=50

Outputvm.vfs_cache_pressure = 50

Again, this is only valid for our current session. We can change that by adding it to our configuration file like we did with our swappiness setting:

sudo nano /etc/sysctl.conf

At the bottom, add the line that specifies your new value:

vm.vfs_cache_pressure=50

Save and close the file when you are finished.

That’s it Your’e done!!!

Photo by bruce mars on Unsplash

Thanks for reading..happy coding

--

--