How To Add Swap Space on Debian 11

There are times when using the system you may encounter an out-of-memory error. One of the possible solutions is to increase or create a SwapFile that will add swap space on Debian 11. This will be the purpose of this post.

What is Swap?

This is a concept very much associated with Linux and could be the subject of a post in itself. However, I will try to address it easily and quickly so that you have the general notions about it.

First, according to the Wikipedia:

Swappiness is a Linux kernel parameter that controls the relative weight given to swapping out of runtime memory, as opposed to dropping pages from the system page cache, whenever a memory allocation request cannot be met from free memory. Swappiness can be set to values between 0 and 200 (inclusive)

To do this we can create a Swap partition or Swap File that will allow us to free up some memory in the case of applications that require it. Keep in mind that this is an emergency and circumstantial process because the RAM will always be much faster than the hard disk. So, abusing this feature could actually slow down the performance of the system.

So, let’s go for it. Let’s add Swap Space on Debian 11

Add Swap Space on Debian 11

The first thing we need to do is to make sure we don’t have Swap on the system.

To do this, open a terminal and run this command

sudo swapon --show

If you don’t get any output on the screen, then there is no Swap on the system.

Another command that can give you the requested information is this one

free -h

Output:

Swap: 0B 0B 0B 0B

This indicates that there is no active Swap partition or Swap file.

Now create a Swap file of whatever size you want or think your system needs. For this example, it will only be 1Gb

sudo fallocate -l 1G /swapfile

After this, assign appropriate permissions

sudo chmod 600 /swapfile

Now we have to tell the system that this file will be for the swap. This can be done with the command mkswap.

sudo mkswap /swapfile

Now you have to activate it as follows

sudo swapon /swapfile

At this point, you will be able to see the changes

sudo swapon --show

Sample Output:

/swapfile file 1024M 0B -2

These changes will work until the session ends. If you want to make them permanent then we have to modify the fstab file.

First, make a backup of it so that if there are any problems you can revert the changes.

sudo cp /etc/fstab /etc/fstab.bak

Now edit it with nano

sudo nano /etc/fstab 

And at the end of the file add

/swapfile none swap sw 0 0

Save the changes and close the editor.

This is enough

Conclusion

Memory management in Linux is flexible and proof of this is that we can add swap space on Debian 11 and the process is quite simple.

So, share this post, and help us grow. Let’s go for more.

Scroll to Top