Overview

Swap space is used to extend the amount of memory(RAM) available for running programs. Without swap space, if you run out of memory applications will fail or be terminated. With available swap space, Linux can take less frequently accessed memory pages and write them to disk, allowing more space for running applications.


There are two different ways in which swap can be managed in Manjaro

  • A swap partition
  • A swap file


This article is not a comprehensive guide to swap. It is intended to serve as a basic primer. More detailed information can be found in the articles linked below

Do I Need Swap

In most circumstances swap is not absolutely required unless you are running out of memory(RAM). However, even with plenty of available memory, it is often used as a safety net or due to specific application requirements.


How Much Swap do I Need

The amount of swap you need is highly variable based on the specific applications and workload. There is no way to provide a universal on swap size recommendation without monitoring usage over a period of time. A reasonable place to start would be the smaller of your total system memory(RAM) or 8GiB.


Hibernation Considerations

If you plan to enable hibernation it is recommended to have at least as much swap as you have RAM.


Displaying Swap Information

The command swapon will display your current swap information. For example:

swapon
NAME      TYPE      SIZE USED PRIO
/dev/sda3 partition 8.4G   0B   -2


Using a Swap Partition

A swap partition is the traditional way of managing swap. In this scenario, a dedicated partition or partitions are created for holding swap.


Creating and Enabling a Swap Partition

To create a swap partition, you first need an available partition. This can be created in any disk management tool and should be set as type Linux Swap.


Once you have a swap partition you will need to initialize the swap partition with mkswap. For example, if your swap partition is /dev/sda3, you could use the command:

sudo mkswap /dev/sda3


Next we need to enable the swap partition with the swapon command. Following our example above this could be done with:

sudo swapon /dev/sda3


In order to ensure that the swap is enabled at boot we can add an entry to /etc/fstab. It is best to use the UUID instead of the device name for this purpose. You can add the line to fstab manually or using the command:

sudo bash -c "echo UUID=$(lsblk -no UUID /dev/sda3) none swap defaults 0 0 >> /etc/fstab"


If you would like to have more than one swap partition, simply repeat the steps above for any additional partitions.


Note
Be sure to replace /dev/sda3 in the above commands with your actual swap partition.


Using a Swapfile

Using a swap partition has one major disadvantage. Changing the size of swap or adding swap requires repartitioning the disk. In current Linux kernels, it is possible to use a swap file instead of a dedicated partition.


Creating and Enabling a Static Swapfile

Tip
There is no reason you can't have both a swap partition and a swapfile. This is an easy way to add more swap without repartitioning


First create and intialize the file to hold the swap. For example, to create a 4GB swapfile, you could use the command:

sudo fallocate -l 4G /swapfile
sudo mkswap /swapfile


Set the appropriate permissions on the file. It should be readable and writable only by root. This can be done with the command:

sudo chmod u=rw,go= /swapfile


Next we need to enable the swapfile with the swapon command. Following our example above this could be done with:

sudo swapon /swapfile


In order to ensure that the swap is enabled at boot we can add an entry to /etc/fstab. You can add the line to ftab manually or using the command:

sudo bash -c "echo /swapfile none swap defaults 0 0 >> /etc/fstab"


Swapfiles on BTRFS

As of kernel 5.0, swapfiles are supported on btfrs. They still require some special handling in addition to the above steps.


Prior to running the fallocate step above, you should run these commands:

sudo truncate -s 0 /swapfile
sudo chattr +C /swapfile
sudo btrfs property set /swapfile compression none


These commands create an empty swapfile, disable COW for that file and ensure that compression is disabled.


Swapfiles on ZFS

zfs doesn't support swapfiles, however you can achieve a similar benefit using a zvol as a swap volume.


Detailed instructions on how to accomplish this can be found in this ZoL guide.


Automated Swap Management with systemd-swap

Note
Using systemd-swap with on btrfs/zfs or with hibernation support requires special handling beyond the scope of this article


It is possible to automatically manage the size of your swapfile using systemd-swap. It can create swap files dynamically based on memory needs. It is also an easy way to enable/manage zswap


Installing and Configuring systemd-swap

First, install and enable systemd-swap

pamac install systemd-swap
sudo systemctl enable systemd-swap.service


Next enable dynamic swap management by creating a file /etc/systemd/swap.conf.d/myswap.conf with the following parameters:

zswap_enabled=0
zram_enabled=0
swapfc_enabled=1


If you would prefer a single command to create this file you could use:

sudo bash -c 'echo -e "zswap_enabled=0\nzram_enabled=0\nswapfc_enabled=1" > /etc/systemd/swap.conf.d/myswap.conf'


Remove Other Swap

In order for systemd_swap to be effective you need to remove any traditional swap devices you have.


To display your current swap devices you can use the command swapon. For example, my test machine displays the following:

swapon
NAME      TYPE      SIZE USED PRIO
/swapfile file        4G   0B   -2
/dev/sda3 partition 8.4G   0B   -3


In this example there are two swap devices. A swapfile and a swap partition. To turn them off we can use the command swapoff.

sudo swapoff /swapfile
sudo swapoff /dev/sda3


Next we need to remove them. The swap partition will need to be deleted using your favorite partitioning tool. You can remove the swapfile by simply deleting it.

sudo rm /swapfile


We need to also remove any lines related to swap from /etc/fstab. Edit the file with an appropriate text editor and remove or comment out the lines for the swap space. In the example above, we would remove these lines:

UUID=c2430cf1-8ea9-4422-a5c5-5a38779194c3 swap           swap    defaults,noatime 0 2
/swapfile none swap defaults 0 0


Next reboot. After the reboot you should see something similiar to the following when running swapon

NAME                           TYPE SIZE USED PRIO
/var/lib/systemd-swap/swapfc/1 file 512M   0B   -2


systemd-swap will add and remove swapfiles as your memory usage dictates.


Note
If you don't delete your existing swap partition it will be automatically activated by systemd even though you have removed it from fstab


Using zswap with systemd-swap

zswap is compressed swap kept in memory(RAM). It keeps the most frequently used pages in RAM and writes less frequently used pages to the swap space on disk. In many workloads, this will result in increased swap performance.


To enable zswap with systemd-swap, simply alter the instructions above and set zswap_enabled to 1

Tuning & Performance Considerations

Although swap seems like a great way to expand memory, excessive swap use will cause severe performance degradation.


There are couple of parameters that can be used to tune swap utilization. These are swappiness and vfs_cache_pressure. To see your current settings for these you can use the following commands:

cat /proc/sys/vm/swappiness
cat /proc/sys/vm/vfs_cache_pressure


swappiness controls how likely a page is to be transferred to swap. The valid range is 0-200 and higher numbers indicate more frequent swaps.


vfs_cache_pressure is a percentage value that controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects. The default value is 100. Increasing this value will increase the rate in which these objects are removed from the RAM cache. Decreasing it will allow these objects to be cached in memory longer, consuming additional RAM over time. Depending on your specific workload, increasing or decreasing this value too far can have significant negative impacts on system performance. Experimentation is needed to find the appropriate balance and the default value is reasonable. In general, it is more common to optimize swappiness before experimenting with vfs_cache_pressure.


To set these values you can use the command sysctl. For example, to set the swappiness value to 20 you could use:

sudo sysctl vm.swappiness=20


There is no preset defined answer on the optimal values for these parameters. Experimentation is needed to find the optimum configuration for your hardware and workload.


See Also