Please note, some of the markdown inside this file does not meet the documentation provided by Linode to help improve the readability of the document on GitHub Gist.
I will happily modify these to meet the style specification from Linode.
---
author:
name: Brendan Jennings
email: jbrendan70@outlook.com
description: 'How to configure swap space in Ubuntu 20.04'
keywords: ["swap", "memory", "virtual memory", "low memory"]
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
published: 2021-03-11
modified_by:
name: Brendan Jennings
title: 'How to configure swap space in Ubuntu 20.04'
contributor:
name: Brendan Jennings
link: https://www.linkedin.com/in/brendan-jennings-3a2a7a199/
---
The steps in this guide require root privileges. Be sure to run the steps below as root
or with the sudo
prefix. For more information on privileges, see our Users and Groups guide.
-
Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode's hostname and timezone.
-
This guide will use
sudo
wherever possible. Complete the sections of our Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services.
Swap is a space on a disk used as virtual memory. When the Linux host is or out of memory, having swap allows the system to move inactive pages from RAM to the swap space on disk.
Swap spaces can be identified by either a file or as a partition.
The main reason people tend to use swap is to increase the amount of memory beyond what is physically installed (RAM) and to enable suspend-to-disk or 'Hibernate' support in Linux.
Well, if swap just allows people to add more 'memory' to the machine, why don't we use this instead of paying for more memory?
People commonly try to use swap as an active extension of memory, and this is an issue for multiple reasons, here's two you should keep in mind:
-
Thrashing
When swap and memory become near-full the system spends so much time paging blocks of memory between your RAM and swap space it struggles to get actual work in!
-
Speed
Physical memory is designed to store temporary data which can be transferred at high-speeds. Swap however exists on your local disk, which aims for persistance and reliability over speed.
Relying on swap space could cause performance issues which could prove detrimental in production environments.
I wouldn't recommend against swap, it's an extremely useful tool- but ensure to use it properly.
This varies system-to-system, hosts with lower amounts of physical memory usually should have more allocated than ones with higher amounts.
Below is a table with an idea of how much memory you should go about allocating to your Linode.
Physical memory | Recommended allocation |
---|---|
1-4GiB | 2 x Physical memory |
4-8GiB | 1.5 x Physical memory |
16-32GiB | 1 x Physical memory |
32-64GiB | 0.5 x Physical memory |
Over 64GiB, you shouldn't really need swap space that much, although it's great to have to move items cached in memory for extended periods of time to disk.
In this example, we'll allocate file-based swap space for a Linode with 8GiB of memory!
-
Pre-allocate the space you want for your swap file to your disk with the
fallocate
utilitysudo fallocate -l 12G /var/swapfile
-
Set the permissions of our swap file to owner (root) write/read only using
chmod
Since our virtual memory is as important as physical memory (in terms of data integrity), let's make sure no unauthorised users can tamper with it.
sudo chmod 600 /var/swapfile
-
Set up the file as a swap area on the device using
mkswap
sudo mkswap /var/swapfile
-
Enable the swap file on your system
We have our swap file ready, but we need to tell our system where it is and to use it!
sudo swapon /var/swapfile
-
(Optional) Make your swap allocation persist
On reboot we want to ensure that our swap file is mounted to the system for usage again.
fstab
(the file systems table), stores information about internal devices for mounting and unmounting.Let's append an entry for our new swap file to fstab.
sudo cp /etc/fstab /etc/fstab.bkp #Backup fstab sudo echo -n '/var/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab #Append fstab
-
(Optional) Check if your swap allocation is functioning
sudo swapon --show
-
Configure the swappiness value for the current state of the system
Below is an example of the swappiness value at 30
sudo sysctl vm.swappiness=30
-
Ensure the swappines value persists by settings it in
/etc/sysctl.conf
Add the line
vm.swappiness=30
to your configuration to set it to 30 on boot.sudo nano /etc/sysctl.conf
It's a common misconception that people believe that the swappiness
value of a Linux system is a threshold of memory where swapping starts.
Linux splits your memory up into zones depending on your system architecture. The zones available are: DMA, DMA32, Normal memory and HighMem (896MiB+ on 32-bit systems).
A zone is attached to node (a group of CPU cores), the kernel then allocates memory for new and existing processes from the nodes associated with a CPU.
Most modern devices only have one node.
Before I explain swappiness itself, I'll go over file pages and anonymous pages.
File page mappings contain raw data from local data from disk, file page mappings are smart, if the data on disk doesn't match the data in memory, when freeing the position in memory it will ensure we write the data to disk. When we need it again we can simply obtain and page the data again.
For anonymous pages, these are memory mappings which are not associated with files or devices. These can be requested on-the-fly, for software or for things like the call stack or heap/free store- practical applications could be running applications requiring more memory or hypervisors.
As we increase our swappiness, the priority of anonymous pages increases, as file pages decrease. With swappiness at 100 anonymous and file pages have the same priority.
Imagine the file priority of swappiness to be how willing the kernel is to free up file pages (pages that hold data which can easily be retrieved as they can be read from disk again, but if altered must write data back) and the priority of anonymous pages to be how much it wants to give up anonymous pages (usually memory allocated to our applications).
The swappiness value represents how likely the system will want to swap file pages between our swap space and physical memory.
Using sysctl
we can experiment with our swappiness value, and see what performs better for our environment.
-
Configure the cache pressure value for the current state of the system
Below is an example of the cache pressure value at 100
sudo sysctl vm.vfs_cache_pressure=100
-
Ensure the cache pressure value persists by settings it in
/etc/sysctl.conf
Add the line
vm.vfs_cache_pressure=100
to your configuration to set it to 100 on boot.sudo nano /etc/sysctl.conf
The vfs_cache_pressure value controls how often the kernel will reclaim memory which is using for dentries (a data structure representing a directory) and inode's (a data structure representing a file), we'll just say 'file pages'.
Decreasing the value makes the kernel prefer to file pages, lowering the amount of reclaimed file pages and if we increase it, this causes the kernel to reclaim file pages more often instead.