Introduction
Before we begin talking about how to add Swap Space on Ubuntu, let’s briefly understand – What is a Swap Space?
Swap space is a vital component of a computer's memory management system. It serves as a temporary storage area where the operating system transfers inactive data from RAM when it becomes full. This frees up RAM for more immediate tasks, preventing system slowdowns or crashes.
Swap space is typically located on a hard disk drive or solid-state drive, and its size can be adjusted to meet the system's requirements. By understanding swap space and its role in memory management, you can optimize your computer's performance and ensure smooth multitasking.
In this tutorial, you will add a Swap file on Ubuntu 22.04. We will also address some FAQs related to the Swap Space on Linux.
Advantages of Swap Space
- Optimal Memory Management: Swap space allows efficient management of memory by offloading inactive data from RAM.
- Increased System Stability: It prevents system crashes and slowdowns by providing additional virtual memory.
- Enhanced Multitasking: Swap space enables smoother multitasking by expanding the available memory capacity.
- Flexibility and Customization: The size of swap space can be adjusted to meet the specific requirements of the system.
- Cost-Effective Solution: Swap space utilizes existing hard disk or SSD storage, reducing the need for costly hardware upgrades.
Few Things to Know Before Beginning
Swap has a slower access time than physical memory, hence it can't be seen as a replacement for physical memory. If your system frequently runs out of memory, you should be adding more RAM.
The size of the Swap file is dependent on the RAM of your system:
- less than 2 GB RAM: Double the amount of RAM
- 2 – 8 GB RAM: Size similar to that of the RAM
- More than 8 GB RAM: Minimum 4 GB of Swap
Only a root or user with sudo privilege can activate the Swap file.
Creating a Swap File
Here, we will create 2 GB
a Swap file. If you want to add more Swap, change the 2G
number per your requirement.
Follow the below steps to add Swap Space on Ubuntu 22.04:
1) Firstly, you need to create a file that will be used as a Swap:
sudo fallocate -l 2G /swapfile
If the fallocate
utility is not present in your system, use the below command instead:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
2) If you want to prevent a regular user from writing and editing the file, set the file permission to 600
.
sudo chmod 600 /swapfile
3) Now, create a Linux Swap area on the file:
sudo mkswap /swapfile
Output
Setting up swapspace version 1, size = 3 GiB (2147479552 bytes)
no label, UUID=fde7d2c8-06ea-400a-9027-fd731d8ab4c8
4) After that, run the following command to activate the Swap file:
sudo swapon /swapfile
If you want to make a permanent change, you need to open the /etc/fstab
file:
sudo nano /etc/fstab
and paste the following line:
/swapfile swap swap defaults 0 0
5) To verify if the Swap is active, either run the swapon
or the free
command:
sudo swapon --show
Output
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -1
sudo free -h
Output
total used free shared buff/cache available
Mem: 981Mi 97Mi 68Mi 0.0Ki 814Mi 735Mi
Swap: 2.0Gi 10Mi 1.9Gi
Changing the Swappiness Value
The Swappiness value can vary between 0 and 100. 0 Swappiness value means it will tell the kernel to aggressively avoid swapping out for as long as possible. On the contrary, a value of 100 will aggressively be swapping the processes out of the physical memory.
Accessing Swap memory is much slower than accessing the physical memory directly. Lower swappiness parameters will improve overall system performance. For normal desktop installation, 10
swappiness is recommended. For database servers, 0 or 1 is recommended.
The optimal swappiness value will depend on your system workload as well as the size of the RAM memory. You should adjust this parameter in small increments, which will help to find an optimal value.
Generally the swappiness value of 60
is fine but for a production server, you may want to lower the value.
3) If you want to set the swappiness value to 10 at runtime. Type the following command as a root
or sudo
user:
sudo sysctl vm.swappiness=10
4) Then, to make the swappiness parameter persistent across the reboots, Open /etc/sysctl.conf
file with your text editor:
sudo nano /etc/sysctl.conf
5) After that, locate vm.swappiness
parameter and proceed to change its value. If this parameter does not exist then, append the below line to the file:
vm.swappiness=10
Steps to Remove a Swap File
If you want to deactivate or delete the swap file, follow the below steps:
- Firstly, you need to deactivate the Swap space:
sudo swapoff -v /swapfile
2. After that, remove the swap file entry /swapfile swap swap defaults 0 0
from the /etc/fstab
file.
3. Use the rm
command to remove the Swap file:
sudo rm /swapfile
FAQs to Add Swap Space on Ubuntu 22.04
How to reduce Swappiness?
The best thing is changing the swappiness to a lower level, like 10. Then, the user will save the file and close it. After rebooting, swappiness will be set to 10. The command is sudo cat /proc/sys/vm/swappiness
.
Does Swappiness need a reboot?
The value for swappiness can be set with a single terminal command. Users should only run the command if they are familiar with running terminal commands. A reboot is not necessary after running the command.
How do I check if I have swap space on my Ubuntu 22.04 system?
You can use the free
command to check if you have any swap space allocated on your Ubuntu 22.04 system. Simply open the terminal and type free -h
. If you have swap space, it will be listed under the Swap:
column.
Can I change the size of my swap space on Ubuntu 22.04 after installation?
Yes, you can change the size of your swap space on Ubuntu 22.04 after installation. To do so, you need to either resize the swap partition or create a new swap file with the desired size and activate it using the swapon
command. Remember to update the /etc/fstab
file to reflect the changes.
How can I monitor my Ubuntu 22.04 system's swap usage?
You can monitor your Ubuntu 22.04 system's swap usage using the free
command or the top
command. Both commands display information about the system's memory usage, including swap usage. You can also use the vmstat
command to get more detailed information about the system's memory usage.
What happens if I run out of swap space on my Ubuntu 22.04 system?
sIf you run out of swap space on your Ubuntu 22.04 system, the system may become unresponsive or even crash. The operating system will start to kill processes to free up memory, which can lead to data loss or corruption. Therefore, it is essential to ensure that your system has enough swap space to prevent such scenarios.
Conclusion
We hope this detailed tutorial helped you to add Swap space on Ubuntu 22.04.
If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.