How to add swap space on Ubuntu
What is swap space/memory?
When the amount of physical memory (RAM) is full, swap space in Linux is used. Non-active pages in memory are moved to the swap memory if the system requires more memory resources and also the RAM is complete.
Swap space can take the form of either a dedicated swap partition or a swap file. Typically, when running Ubuntu on a virtual machine, a swap partition is not present, and the only option is to create a swap file.
Before You Begin
Swap should not be seen as a replacement to physical memory. Since swap space is a section of the hard drive, it has a slower access time than physical memory. If your system constantly runs out of memory, you should add more RAM.
How to calculate the needed swap space in Ubuntu?
Generally, the size of the swap file depends on how much RAM your system has:
The swap space in Ubuntu should equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB. For Ubuntu systems with really large amounts of RAM (more than 32 GB), you can likely get away with a smaller swap partition (around 1/2 half the physical RAM).
// M = Amount of RAM in GB.
// S = Amount of Swap Memory in GB.
if (M < 2):
S = M * 2
else if (M > 32):
S = M / 2
else:
S = M
So we can say:
- A system with 2 GB of physical RAM would have 4 GB of swap memory.
- A system with 3 GB of physical RAM would have 3 GB of swap memory.
- A system with 32 GB of physical RAM would have 16 GB of swap memory.
Creating a large swap space partition can be especially helpful if you plan to upgrade your RAM at a later time.
How to check if my Ubuntu installation already has swap enabled ?
Before you start, check if your Ubuntu installation already has swap enabled by typing: cat /proc/swaps
[root@krython ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 2248700 0 -2
Alternatively, use the swapon --show
command:
[root@krython ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2248700 0 -2
If the outcome is empty it means that your system does not have swap space enabled.
Creating the swap file
The user that you are logged in as need to be a root user or need to have sudo privileges to be able to include and also create swap space.
In this tutorial, we will add 2GB
of Swap memory to our Ubuntu System.
Example: 12G if you want a 12GB of Swap space.
Let's begin:
- Install the fallocate command, we will use it to create the swap file:
sudo apt-get install util-linux -y
- Create the swap file that will be used as a Swap space:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
Let me break down the command for you:
bs=1024: This sets the block size to 1024 bytes.
count=2097152: This sets the number of blocks to 2097152.
Multiplying the block size by the number of blocks gives you the total size of the file:
1024bytes/block×2097152blocks=2147483648bytes
- Ensure that just the root user can read and also write the swap file by setting the correct permissions:
sudo chmod 600 /swapfile
- Set up the file as swap:
sudo mkswap /swapfile
- Activate the swap space A short-lived turn on (lost on reboot):
sudo swapon /swapfile
- Customize the
/etc/fstab
to immediately add the new swap at system boot time by adding the following line:
/swapfile swap swap defaults 0 0
- Confirm that the swap is active by using either the
swapon
or thefree
command:
[root@krython ~]# sudo swapon -s
NAME TYPE SIZE USED PRIO
/swapfile partition 2.1G 0B -2
[root@krython ~]# free -m
total used free shared buff/cache available
Mem: 809 160 258 5 389 521
Swap: 2195 0 2195
What is swappiness, how to change it's value?
Swappiness is a Linux kernel property that sets the balance between swapping out pages from the physical memory to the swap space and removing pages from the page cache. It basically defines how often the system will use the swap space.
swappiness
can have a value of between0
as well as100
swappiness=0
tells the kernel to avoid swapping processes out of physical memory for as long as feasibleswappiness=100
informs the kernel to boldy swap processes out of physical memory as well as relocate them to swap cache
The default setup in Ubuntu is swappiness=30
.
Lowering the default value of swappiness will possibly boost general performance for a regular Ubuntu desktop setup.
A value of swappiness=10
is suggested, yet feel free to experiment.
Remember: Ubuntu server installments have different performance requirements to desktop systems, and likewise the default value of 30
is likely preferable.
- Check the swappiness value:
cat /proc/sys/vm/swappiness
- Change the swappiness value A temporary change (lost on reboot):
sudo sysctl vm.swappiness=60
- Make the change permanent, edit the configuration file located in
/etc/sysctl.conf
- Search for
vm.swappiness
and change its value as desired. Ifvm.swappiness
does not exist, add it to the end of the file like so:
vm.swappiness=60
- Finally, reboot your system:
sudo reboot
Remove a swap file
You will do the opposite steps of the setup process, deactivate swap space then remove the swap file
- Deactivating the swap space:
sudo swapoff -v /swapfile
- Remove the swap file entry from the
/etc/fstab
file:
- Finally, Delete the swap file:
sudo rm -rf /swapfile
Conclusion
- You have learned how to create a swap file and also activate it on your Ubuntu system.
- You have learned how to change the swappiness value on your Ubuntu system.
- You have learned how to deactivate swap space as well as remove your swap file on your Ubuntu system.