Knowledge Base

Browse our knowledge base for free solutions to common problems

Enabling File Based Swap On Linux

Created On: 20 February 2023
Written by: Ben

Introduction

Linux swap is a space on a Linux system's hard drive that is used as virtual memory. It is an area of the hard drive that is set aside to temporarily store data that cannot be held in RAM (Random Access Memory), either because the RAM is full or because the data is not accessed frequently enough to justify keeping it in RAM.

When the system runs out of RAM, it moves some of the less frequently used data to the swap space to free up RAM for more critical tasks. This process is called swapping. When the data that was swapped out is needed again, it is moved back into RAM, and other data is moved to the swap space.

The swap space is usually a partition on the hard drive, although it can also be a file in the file system. It is recommended to have a swap space that is at least as large as the system's RAM size, although the exact size of the swap space needed depends on the system's usage patterns and workload.  If you are unsure how much swap space to allocate, for reference below we have included simple table to determine this.

Considerations

In general, swap on an SSD (Solid State Drive) can perform better than on a traditional hard disk drive (HDD) due to the faster read and write speeds of SSDs. This can result in improved performance when the system needs to swap data between RAM and the swap space.

SSDs use flash memory to store data, which can be accessed more quickly than the spinning disks used in HDDs. This means that when the system needs to swap data, it can do so more quickly on an SSD, resulting in less delay or lag time.

However, it's important to note that excessive swapping can still be a performance issue, regardless of whether it's on an SSD or HDD. When the system is constantly swapping data between RAM and the swap space, it can slow down overall performance. Therefore, it's important to have enough RAM for the system's workload to minimize the need for swapping in the first place.

Determine Swap Space To Allocate

As a quick guide you can use the following table to determine the 'recommended' swap for modern Linux distros:

Amount of RAM installed on systemRecommended swap spaceRecommended swap space with hibernation
<= 2GB2x RAM3x RAM
2GB to 8GB= RAM2x RAM
8GB to 64GB4GB to 0.5x RAM1.5x RAM
> 64GBMinimum 4GBHibernation not recommended
Table showing advised SWAP partition / files sizes in Linux

Pre-Checks before creation of swap file

See if the system already has swap

Check to see if the system in question has swap already enabled with:

sudo swapon --show

If nothing is returned from the above command then the likelihood is that swap is not enabled on the system.

To verify this you can use the following command:

sudo free -h
root@s1 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1789         517         831           8         441        1110
Swap:           0            0           0

If you see swap 0 0 0 this mean no swap space has been allocated.

Determine if we have enough physical disk space

Next we should determine if we have enough physical disk space to allocate our swap file, to do this run the following command:

sudo df -h

Search for the partition what has /mounted and check the Avail column:

[root@s1 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        870M     0  870M   0% /dev
tmpfs           895M     0  895M   0% /dev/shm
tmpfs           895M  8.5M  887M   1% /run
tmpfs           895M     0  895M   0% /sys/fs/cgroup
/dev/sda1        20G  5.1G   15G  26% /
tmpfs           179M     0  179M   0% /run/user/1001

In the example above the line we would look at is:

/dev/sda1        20G  5.1G   15G  26% /

As we are allocating a 2GB swap file there is plenty of space available (15GB).

Enabling file based swap

Create our swap file

First we allocate the file that is going to be used for swap:

sudo fallocate -l 2G /swapfile

Apply correct permissions to swap file

First change the permissions of the file for security:

sudo chmod 600 /swapfile

Transform file to swap

Now let the system know we are going to use the file for swap:

sudo mkswap /swapfile

Enable the swap file

Enable the newly generated file for swap usage:

sudo swapon /swapfile

IMPORTANT: Some Linux distros do not like the way the swapfile has been generated. If you receive an error similar to the following at this stage you need to generate the swapfile with /dev/zero instead:

swapon: /swapfile: swapon failed: Invalid argument

Example of generating the swap file with /dev/zero:

dd if=/dev/zero of=/swapfile bs=1M count=2048

Where count= is the amount of space to allocate in M.

Examples:

  • 1GB = 1024 = (dd if=/dev/zero of=/swapfile bs=1M count=1024)
  • 2GB = 2048 = (dd if=/dev/zero of=/swapfile bs=1M count=2048)
  • 3GB = 3072 = (dd if=/dev/zero of=/swapfile bs=1M count=3072)
  • 4GB = 4096 = (dd if=/dev/zero of=/swapfile bs=1M count=4096)

A quick fix is to run the following then proceed with this article:

sudo swapoff /swapfile
sudo rm -rf /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile

Verify that swap is on with the following:

sudo swapon --show

Output:

[root@s1 ~]# sudo swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -2
[root@s1 ~]#

Check to see if the swap file is appearing with free -h:

free -h

Output:

[root@s1 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.7G        518M        829M        8.4M        441M        1.1G
Swap:          2.0G          0B        2.0G
[root@s1 ~]#

Ensure swap is enabled on boot

Linux uses fstab to mount the partitions which are used on boot. We need to ensure that we add our newly created swap partition to this list of partitions.

Firstly backup the current fstab configuration with the following:

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

Run the following command which will use echo to print a new line to the current fstab configuration to include our newly created swap partition:

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

Tuning swap settings

You can skip this step if the machine you have applied swap to is a desktop environment.

The default swap settings are geared towards desktop environments that require hibernation therefore changing them should only be done in exceptional circumstances.

In our case the Linux distro in question is a server using a minimal linux install. Tweaking the swap settings will benefit the servers performance.

Adjusting swappiness

Swappiness is a property in Linux that controls the degree to which the kernel will use swap space. It is a parameter that determines how aggressively the system will swap data from RAM to the swap space when the amount of free memory falls below a certain threshold.

The swappiness value is a number between 0 and 100 that represents the kernel's preference for using swap space. A higher value means that the kernel is more likely to use swap space, while a lower value means that the kernel will try to avoid using swap space as much as possible.

By default, the swappiness value is set to 60. This means that the kernel will start swapping out data when the amount of free memory falls below 40% of the total system memory. However, this value can be adjusted depending on the specific needs of the system.

If the system has plenty of RAM, a lower swappiness value can be set to avoid unnecessary swapping and preserve the performance of the system. On the other hand, if the system has limited RAM or is running memory-intensive applications, a higher swappiness value may be necessary to prevent the system from running out of memory and crashing.

Confirm the current swappiness value with the following:

cat /proc/sys/vm/swappiness

Output:

[root@s1 ~]# cat /proc/sys/vm/swappiness
60

For desktop 60 is a very good value, for a server we need to change this closer to 10 (10%). Do do this we can use sysctl:

sudo sysctl vm.swappiness=10

Using sysctl will only allow this setting to be present until next reboot. To apply the change permanently we need to amend /etc/sysctl.conf :

sudo nano /etc/sysctl.conf

Now add the following to the bottom of the file:

vm.swappiness=10

Now when the server / desktop is rebooted our swapiness setting will be persistent.

Adjusting swap cache pressure

Another important parameter we need to change is the vfs_cache_pressure setting. vfs_cache_pressure is a kernel parameter in Linux that controls how aggressively the kernel will reclaim memory that is used for the file system cache. The file system cache is a mechanism used by the kernel to keep frequently accessed files and directories in memory to speed up access times.

When the system runs low on memory, the kernel will try to free up memory by evicting pages from the file system cache. The vfs_cache_pressure parameter determines how aggressively the kernel will do this by setting the ratio of inodes and dentries to pages of application memory when deciding which pages to evict.

A high value of vfs_cache_pressure (e.g. 100) means that the kernel will try to aggressively reclaim memory from the file system cache, which can result in faster performance for applications but may cause a higher load on the file system. Conversely, a low value of vfs_cache_pressure (e.g. 0) means that the kernel will be less aggressive in reclaiming memory from the file system cache, which can improve performance for file system operations but may result in slower application performance.

The default value of vfs_cache_pressure is 100, which means that the kernel will be fairly aggressive in reclaiming memory from the file system cache. However, the value can be adjusted according to the specific needs of the system by editing the /etc/sysctl.conf file or using the sysctl command.

You can check the current vfs_cache_pressure with the following:

cat /proc/sys/vm/vfs_cache_pressure

Output:

[root@s1 ~]# cat /proc/sys/vm/vfs_cache_pressure
100
[root@s1 ~]#

For desktop 100 is a not a bad value, for a server we need to change this closer to 50 (50%). Do do this we can use sysctl:

sudo sysctl vm.vfs_cache_pressure=50

To make the settings persistent on boot we need to add the changes to sysctl.conf like before. Open the file:

sudo nano /etc/sysctl.conf

Now add the following line to the bottom:

vm.vfs_cache_pressure=50

Quick Application

In this section we provide a series of commands to quickly apply and configure swap without reading the details of the article (proceed with caution):

For server:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo cp /etc/fstab /etc/fstab.bak
sudo echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo cp /etc/sysctl.conf /etc/sysctl.conf.bak
sudo echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf

For desktop:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo cp /etc/fstab /etc/fstab.bak
sudo echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Remember to change count= to the amount of space you wish to allocate for swap.

Conclusion

Following the steps in this guide will ensure you have a little breathing space and help reduce the frequency you get out of memory errors. It should also allow us to run some larger applications on more budget hardware and should provide you improved performance system wide.

The fact that we use file based swap also provides a greater level of flexibility, we can increase and decrease the size of swap at any given time.

ICTU LTD is a company registered England and Wales (Company No. 09344913) 15 Queen Square, Leeds, West Yorkshire, England, LS2 8AJ
Copyright © 2024 ICTU LTD, All Rights Reserved.
exit