One of the recommended ways of boosting your server’s performance and avoiding running out of memory is by creating a swap space or swap file. Swap is virtual memory located on your hard disk and extends the RAM when the memory utilization is high.
When your system is running out of the main memory, the Linux kernel swaps a portion of memory from the RAM and writes it onto the hard disk. This way, more RAM is made available to the system and processes can run smoothly.
In this tutorial, we learn how to create a swap file on Linux distributions such as Ubuntu and CentOS.
Check swap configured
Having looked at the advantages of creating a swap file, it’s important to first confirm if swap is configured on your system. This will help you to know whether or not to configure a swap file.
To check if swap space is created, run the following command :
The command will display the size of the swap partition and the filesystem label. Here’s some output of the command.
NAME TYPE SIZE USED PRIO /dev/sda9 partition 3.8G 383.3M -2
If you run the command and you get no output, then it implies that you don’t have a swap partition configured. You can confirm the absence of swap space by running the following command:
$ free -h
This displays the statistics of the main memory and swap utilization.
total used free shared buff/cache available Mem: 7.6G 5.6G 386M 1.1G 1.7G 675M Swap: 0B 0B 0G
From the output, we can clearly see that in the ‘swap’ row, no memory is printed. This is a confirmation that there’s no active swap partition residing on the Linux system. Let’s now create a swap file for the system.
Check hard disk space
As earlier discussed, swap space is virtual memory residing on the hard disk that provides an extension to the RAM. With that in mind, we are going to create a swap file on the hard disk. But before we do so, it’s always prudent to check the available hard disk space.
To check the available hard drive space on Linux, run the following command:
$ df -h
In the output, be on the lookout for the root partition denoted by a single forward-slash ( / ) under the ‘Mounted on‘ column. In the output below, the root partition is labeled as /dev/sda7 filesystem and has 97G free space which is more than enough.
Filesystem Size Used Avail Use% Mounted on udev 3.9G 0 3.9G 0% /dev tmpfs 784M 2.3M 781M 1% /run /dev/sda7 156G 54G 97G 39% / tmpfs 3.9G 828M 3.1G 22% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda10 29G 5.9G 21G 22% /var /dev/sda8 1.9G 124M 1.7G 7% /boot
Create a swap file
Having determined that we have sufficient space on our Linux system, we are going to proceed with creating a swap file. Remember, swap space is usually created during installation. If swap space already exists, then you are good to go. However, if you have no swap space, it’s recommended to create a swap file.
With that in mind, we are going to create a swap file of 1 GB. To achieve this, issue the fallocate command as shown:
$ sudo fallocate -l 1G /swap_file
Feel free to give the swap file whatever name you like. In this case, we have named it swap_file. However, if the fallocate utility is not present, execute the following command:
$ sudo dd if=/dev/zero of=/swap_file bs=1024 count=1048576
Next, set the correct file permissions for the swap file since only the root is allowed read and write permissions:
$ sudo chmod 600 / swap_file
The chmod 600 argument assigns read and write permissions only to the swap file.
Setup swap area
To set up the swap area, use the following command using the mkswap utility as shown:
$ sudo mkswap / swap_file
To activate the swap file run the following command:
$ sudo swapon /swap_file
Making Swap Persistent on Reboot
The swap file created on the Linux system is not permanent and will not survive a reboot. To make the swap file persistent even upon a reboot, we need to edit the /etc/fstab file and append the newly created swap entry.
Using your preferred text editor, open the /etc/fstab file. We have used vim editor as shown:
$ sudo vim /etc/fstab
Next, paste the following content
/swap_file swap swap defaults 0 0
Save the changes and exit the /etc/fstab file. To confirm that swap file has been created run the following command:
$ sudo swapon --show
You should get output that is similar to what we have
NAME TYPE SIZE USED PRIO /swap_file file 1024M 346.4M -2
At this point, we have successfully created our swap file. We are going to consider one more property of swap and that is the swappiness value. Swappiness refers to a property in the Linux kernel that determines the frequency of swap usage.
The swappiness value ranges from 0 to 100. A low value implies that the system will cause the kernel to keep off swapping as much as possible. A higher value has the opposite effect – it makes the kernel to use swap as frequently as possible.
By default, the swappiness value is 60. You can confirm this by running the command:
Whilst this value may seem ok, it’s not preferred for production servers. To set a lower value, say 20, run the following command.
sudo sysctl vm.swappiness=20
For this value to remain persistent upon rebooting, add the following line to the /etc/sysctl.conf file
Swap Files vs. Swap Partitions
Linux comes with 2 kinds of swap space, swap partition and swap file. A swap partition is a distinct part of the hard drive that is purely used for swapping. No files exist here.
On the other hand, a swap file is a unique file that sits on the filesystem alongside your data and system files.
Swap space really needed? how much?
The very commonly asked question when installing Linux is ‘Do I need to create swap?’. As we earlier discussed, swap space is meant to act as a buffer when the RAM gets exhausted for one reason or the other. When there is no more memory to accommodate applications then the operating system chooses a certain process to kill. This will start to affect the performance of the system.
Well, chances are that you may never entirely use up all that RAM. However, to be on the safe side, it’s worth having a bit swap space. Think of swap space as a safety boat in a yacht. In the unlikely event that the yacht catches fire, you can use it to sail to safety.
So here comes the big question, How much swap space do I need to create? To be candid, there isn’t a straight answer to this question, just recommendations. RedHat’s suggestion, recommends the creation of swap space that is 20% of the Physical RAM installed on a Linux system. However, this can differ depending on the size of the RAM and operating system.
For example, CentOS recommends the following:
- If the RAM is less than 2GB, then swap space should be twice the size of RAM.
- If the RAM is more than 2GB, then swap space = 2 + RAM size. For example, if the RAM is 4 GB, swap space should be 6 GB.
For Ubuntu, this is slightly different
- If the RAM is less than 1GB, the swap space should be equal to or double the RAM size.
- Otherwise, if the RAM is more than 1GB, swap space should be equal to the square root of RAM.
In hibernation mode, swap space is essential for the very reason that it preserves the state of RAM by transferring the content located on the RAM onto the swap partition. As a result, it’s recommended that you set the swap size to be equal to the size of the RAM. so, if you have RAM of 8G, a swap file of 8G would suffice.
We cannot emphasize more the need for having swap space on your system. It comes in handy when the main memory is running out and helps you to handle heavy applications such as video editing software. No matter how large the RAM capacity is on your Linux system, it’s always recommended to configure swap for any unforeseen events.
It’s our hope that you can now comfortably create and enable a swap file on your Linux system. Your feedback on this guide will be highly appreciated.