Swap space is a type of memory that can be used as an extension of your system’s RAM. It is used when your system runs low on physical memory and needs to offload some data to continue running smoothly. If you are using a system with limited RAM, it is important to increase the swap space to prevent performance issues.
In this article, we will discuss the steps to add swap space in Ubuntu 22.04.
Steps to Add Swap Space on Ubuntu
Use the following steps to create and add swap memory on your Ubuntu system.
- Creating a new swap file: The first step in adding swap space is to create a new swap file. You can do this using the “fallocate” command. The following command creates a 4 GB swap file:
sudo fallocate -l 4G /swapfile
- Changing file permissions: Next, you need to change the permissions on the swap file to secure it. You can do this with the “chmod” command:
sudo chmod 600 /swapfile
- Marking the file as swap: The next step is to mark the file as swap. This can be done with the “mkswap” command:
sudo mkswap /swapfile
- Enabling the swap file: Once the file has been marked as swap, you need to enable it. You can do this with the “swapon” command:
sudo swapon /swapfile
- Making the swap file persistent: Finally, you need to make the swap file persistent across reboots. To do this, add the following line to the “/etc/fstab” file:1/swapfile swap swap defaults 0 0
- Save the changes and close file.
You have successfully added swap space on your Ubuntu system.
Adjust Swappiness Kernel Parameter
“Swappiness” is a kernel parameter in the Linux operating system that controls the balance between swapping out runtime memory to disk and freeing it up to be used by other processes. The value of the swappiness parameter can be set in the range of 0 to 100 and it determines the preference for swapping out processes to disk as memory usage increases.
A value of 0 means the system will not use the swap space, and a value of 100 means the system will actively use the swap space. A value between 60-80 is a good starting point for most systems.
To adjust the swappiness value in Ubuntu, follow these steps:
- You can use the following command to adjust the
swappiness
value temporarily:sudo sysctl vm.swappiness=10
- To make the changes permanent. open the sysctl configuration file:
sudo nano /etc/sysctl.conf
And add the following line to the file:
1vm.swappiness=10(Replace 10 with the desired value for swappiness, where a value of 0 means the system will not use the swap space, and a value of 100 means the system will actively use the swap space. A value between 60-80 is a good starting point for most systems.)
- Save and close the file.
- Update the sysctl settings:
sudo sysctl -p
- To verify the change, use the following command:
cat /proc/sys/vm/swappiness
Note: The changes made to the swappiness value will take effect after a reboot.
Remove Swap Space on Ubuntu
To remove swap space on Ubuntu, follow these steps:
- First, disable the swap file:
sudo swapoff /swapfile
- Remove the entry for the swap file from the fstab file:
sudo nano /etc/fstab
Locate the line that contains the swap file information and delete it:
1/swapfile swap swap defaults 0 0 - Save and close the fstab file.
- Remove the swap file:
sudo rm /swapfile
Conclusion
In this article, we have discussed the steps to add swap space in Ubuntu 22.04. Increasing swap space is important to prevent performance issues on systems with limited RAM. By following the steps outlined in this article, you can easily add swap space to your system and improve its performance.