Before we start talking on how to add swap space on debian 11, let's briefly understand - What is SwapSpace ?
Swap space is a portion of a hard drive that acts as virtual memory, allowing the operating system to use disk space as additional RAM. Adding swap space in Debian 11 can help improve performance and prevent out-of-memory (OOM) errors.
This tutorial will walk you through the process of adding swap space on Debian 11.
How to Create Swap in Debian 11
You can use a swap file instead of a separate swap partition. Debian virtual systems typically don't have a swap partition, so a swap file needs to be built.
sudo swapon --show
You can also accomplish it by entering the instructions listed below in the terminal.
sudo swapon -s
As you can see, swap memory is not enabled, and the value displayed is 0.
How to create a Swap file
Since there is presently no memory, you must enter the following command in the terminal to generate memory.
sudo fallocate -l 8G /swapfile
The system memory in the images above is around 4 GB, thus it is generally accepted that swap memory should be at least twice that large. However, you can adjust it according to your requirements.
Following that, you must type the Linux swap area into the file. The mkswap command now allows you to convert files to swap memory.
sudo mkswap /swapfile
The swap memory needs to be enabled next, so enter the following command in the terminal to do so:
sudo swapon /swapfile
Setting swapiness value
A Linux kernel feature called swappiness regulates how frequently the swap area is used. Swappiness is measured on a scale from 0 to 100. When the value is low, the kernel will attempt to use swap space as little as possible; however, when the value is high, the kernel will use swap space more intensively.
By entering the following command in the terminal, you can modify this value to suit your needs.
sudo sysctl vm.swappiness=20
The workload on your system and how memory is used define the ideal swappiness number. You should modify this option in small increments to find its proper value.
How to delete a swap file
Enter the command below in the terminal if you need to delete the swap file for whatever reason.
sudo swapoff -v /swapfile
The swap file will then be deactivated, but you can also entirely delete it by using the following command in the terminal.
sudo rm /swapfile
FAQs: Adding Swap Space on Debian 11
Why do I need to add swap space on Debian 11?
Adding swap space on Debian 11 can help improve system performance by providing additional virtual memory. It is particularly useful in scenarios where the system runs out of physical RAM and faces memory-intensive tasks or applications.
How much swap space should I add on Debian 11?
The recommended amount of swap space depends on various factors, such as the amount of RAM installed, the workload of your system, and individual requirements.
What is the recommended method to add swap space on Debian 11?
The recommended method to add swap space on Debian 11 is by creating a swap file. This provides flexibility as you can easily adjust the swap space size if needed.
Can I add swap space using a swap partition instead of a swap file?
Yes, you can add swap space using a swap partition instead of a swap file. This involves creating a separate partition on your hard drive specifically for swap space.
How do I check if the swap space is correctly added on Debian 11?
You can check if the swap space is correctly added by running the
swapon --show command or using tools like
top. These commands will display information about swap usage and availability.
What if I want to remove or disable the swap space on Debian 11?
If you want to remove or disable the swap space, you can use the
swapoff command to turn off the swap space and remove the entry from the
Can I resize the swap space after adding it on Debian 11?
Yes, you can resize the swap space by adjusting the size of the swap file or by creating a new swap partition with a different size.
Adding swap space on Debian 11 can be beneficial for improving system performance and preventing out-of-memory errors in memory-intensive scenarios. By creating a swap file using the recommended method, you can easily adjust the swap space size and persistently mount it during system startup.
In this tutorial, you learned how to generate and benefit from swap space.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them.