Step 1: Check current swap usage
First, you can check the current swap usage to understand if you need to increase swap space:
swapon --show
free -h
Step 2: Create a new swap file
Choose an appropriate size (e.g., 2GB) and use the dd command to create a new swap file. Here's an example for 2GB:
sudo fallocate -l 2G /swapfile
If fallocate is not available, you can use the dd command:
sudo dd if=/dev/zero of=/swapfile bs=1G count=2
Step 3: Set permissions for the swap file
For security reasons, it is necessary to set the correct permissions so that only the root user can access this file:
sudo chmod 600 /swapfile
Step 4: Format the file as swap space
Next, format the newly created file as swap space:
sudo mkswap /swapfile
Step 5: Enable the swap file
Enable the swap file that was just created:
sudo swapon /swapfile
Step 6: Verify the swap space
Check again whether the swap has been successfully added:
swapon --show
free -h
Step 7: Make the swap file mount automatically after system reboot
To ensure that this swap file is automatically mounted on every boot, it needs to be added to the /etc/fstab file. Open that file:
sudo nano /etc/fstab
Add the following line at the end of the file:
/swapfile none swap sw 0 0
Save and exit the editor.
Complete
You have now successfully increased swap space on Debian 12. If you need to adjust the size of the swap, you can delete the existing swap file and then repeat the steps above, specifying the desired new size.
After you create and enable the swap file following the steps above, the swap space will take effect immediately. You can verify it with the following command:
swapon --show
free -h
If you see the new swap space displayed in the output, then it has been successfully enabled.
Notes
- A reboot is only necessary if you have modified the
/etc/fstabfile to ensure the swap file is automatically mounted after a system restart. - If you want to temporarily disable the swap file, you can use the following command:
sudo swapoff /swapfile - To re-enable the swap file, run again:
sudo swapon /swapfile
A swap usage of 0% usually indicates that the system is not currently using swap space. There could be several reasons for this:
1. Good memory usage
If your system memory (RAM) has sufficient free space, the Linux system typically does not actively use swap space. Based on the information you provided earlier, your system has about 85 MiB of free RAM, which means the system does not need to use swap under normal conditions.
2. Swappiness setting
Linux uses a parameter called swappiness to decide when to use swap space. The value of this parameter ranges from 0 to 100:
- A lower value (e.g., 10) makes the system prefer physical memory over swap space.
- A higher value (e.g., 60 or higher) means the system will use swap space more frequently.
You can view the current swappiness value with the following command:
cat /proc/sys/vm/swappiness
If you want the system to use swap space more aggressively, you can consider setting it to a higher value (e.g., 60). To temporarily change the swappiness value, use the following command:
sudo sysctl vm.swappiness=60
To make the change permanent, edit the /etc/sysctl.conf file and add or modify the following line:
vm.swappiness=60
Then apply the change:
sudo sysctl -p
3. Light system load
If your system load is light and there are no large applications or processes running, the system may not need to use swap space at all. In this case, even if you have configured swap space, it will not be used.
4. Applications are not consuming a large amount of memory
If the applications you are running have low memory requirements, the system will not use swap space. The system only starts using swap space when memory usage is nearing its limit.
Summary
If you want the system to use swap space, you can try the following operations:
- Increase the system load by running some applications that require a large amount of memory.
- Adjust the
swappinessparameter to make the system more inclined to use swap space.

Comments are closed
The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.