In Linux, swap space is used to provide you additional ram when there is no more available. The swap space is accessed if the RAM is full and more memory resources are needed. Swap space is available on the system’s hard disk, which is relatively slower than the physical memory. When the operating system recognizes that main memory is low and that extra RAM is necessary to execute applications effectively, it looks for swap space and transfers data there. The swap space is useful for devices with little RAM, but it should not be replaced.
When physical memory is full, swap is a space on the disc that is utilized. A Linux system that runs out of RAM will shift inactive pages to swap space. As an alternative to a dedicated swap partition, you can use a swap file. Generally, Debian virtual systems lack a swap partition, so a swap file must be created instead.
How to Create Swap in Debian 11
Before creating a swap file, you need to verify whether your OS has already swapped space enabled or not. There are several alternatives available, which are outlined below.
$sudo swapon --show
You can also do that by typing the below-mentioned commands in the terminal.
$sudo swapon -s
You can see that there is no swap memory enabled, and the value it is showing is 0.
How to create a Swap file
You need to type the following command in the terminal to create memory since no memory is currently present.
$sudo fallocate -l 8G /swapfile
The above images show that the system memory is around 4GB, so it is a standard rule that swap memory should at least double the system memory size, but you can assign it as per your requirements.
After that, you need to provide the Linux swap area on the file by typing. You can now convert files to swap memory using the mkswap command.
$sudo mkswap /swapfile
The next step is to enable the swap memory, and for that, you need to type the following command in the terminal.
$sudo swapon /swapfile
Modifying swapiness value
Swappiness is a Linux kernel feature that controls how often the swap area is used. Swappiness is a scale that starts from 0 and end at 100. If you set the value low, the kernel will try to use swap space as little as possible, whereas if you set it high, the kernel will make more intensive use of it.
You can change this value as per your requirement by typing the following command in the terminal.
$sudo sysctl vm.swappiness=20
The best swappiness number is determined by the workload on your system and how memory is used. To discover an appropriate value, you should modify this parameter in modest increments.
How to remove a swap file
If you need to delete the swap file for whatever reason, type the following command in the terminal.
$sudo swapoff -v /swapfile
This will deactivate the swap file later; you can also completely remove the file by typing the following command in the terminal.
$sudo rm /swapfile
In Linux, swap space is used when there is no more physical memory (RAM) available. The swap space is accessed if the RAM is full and more memory resources are needed. When the operating system recognizes that main memory is low and that extra RAM is necessary to execute applications effectively, it looks for swap space and transfers data there. We have shown you how to create and utilize swap space to your advantage in this article.