How To Add Swap Space on Debian Linux


Swap space is an essential component in maintaining the performance and stability of a server, especially when it comes to managing out-of-memory errors in applications. In this guide, we will demonstrate how to create a swap file on a Debian 10 server to increase available virtual memory.

Warning: While swap space is generally recommended for systems using traditional spinning hard drives, using swap on SSDs can lead to hardware degradation over time. As a result, we do not recommend enabling swap on servers that utilizes SSD storage, as this may negatively impact the reliability of the underlying hardware for you and your neighbors. This guide is intended as a reference for users who may be using spinning disk systems elsewhere.

What is Swap?

Swap is a designated area on a hard drive where the operating system can temporarily store data that it can no longer hold in RAM. Essentially, this allows you to increase the amount of information that your server can retain in its working “memory,” albeit with some trade-offs. The swap space on the hard drive will primarily be used when there is insufficient space in RAM to store application data that is currently in use.

Although data written to the disk will be considerably slower than data stored in RAM, the operating system will prioritize keeping active application data in memory while using swap for older data. In general, having swap space as a safety net when your system’s RAM is depleted can help prevent out-of-memory exceptions on systems with non-SSD storage available.

Step 1 – Checking the System for Swap Information

Before starting, it’s essential to check whether the system already has swap space available. While it’s possible to have multiple swap files or swap partitions, usually one should suffice but you can app multiple.

sudo swapon --show

If the output is empty, it means there is no swap space configured on your system. If there is already swap space available, the command will display information about the swap, such as its location and size.

You can verify that there is no active swap using the free utility:

free -h

              total        used        free      shared  buff/cache   available
Mem:          990Mi        36Mi       863Mi       4.0Mi        89Mi       837Mi
Swap:            0B          0B          0B

As you can see in the Swap row of the output, no swap is active on the system.

You can check available disc space by running

df -h


Filesystem      Size  Used Avail Use% Mounted on
udev            488M     0  488M   0% /dev
tmpfs           100M  4.5M   96M   5% /run
/dev/vda1        25G  989M   23G   5% /
tmpfs           499M     0  499M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           499M     0  499M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/1001

The device with / in the Mounted on the column is our disk in this case. We have plenty of space available in this example (only 1.4G is used). Your usage will probably be different.

Although there are many opinions about the appropriate size of a swap space, it really depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point. Another good rule of thumb is that anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.

Step 2 – Creating a Swap File

Now that we know our available hard drive space, we can create a swap file on our filesystem. We will allocate a file of the swap size that we want called swapfile in our root (/) directory.

The best way of creating a swap file is with the fallocate program. This command instantly creates a file of the specified size.

Since the server in our example has 1G of RAM, we will create a 1G file in this guide. Adjust this to meet the needs of your own server:

If your system doesn’t have a swap file, you can create one using the following steps:

  1. Choose the desired size for your swap file (e.g., 1GB) and create an empty file using the fallocate command:
    sudo fallocate -l 1G /swapfile

We can verify that the correct amount of space was reserved by typing

ls -lh /swapfile


-rw-r--r-- 1 root root 1.0G May 29 17:34 /swapfile

Our file has been created with the correct amount of space set aside.

Now that we have a file of the correct size available, we can turn this into swap space.

Enabling the Swap File

First, we need to lock down the permissions of the file so that only the users with root privileges can read the contents. This prevents normal users from being able to access the file, which would have significant security implications.

Make the file only accessible to the root by typing:

sudo chmod 600 /swapfile

Verify the permissions change by typing:

ls -lh /swapfile
-rw------- 1 root root 1.0G May 29 17:34 /swapfile

As you can see, only the root user has the read and write flags enabled.

We can now mark the file as swap space by typing:

sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=b591444e-c12b-45a6-90fc-e8b24c67c006f

Verify that the swap is available by typing:

sudo swapon --show
/swapfile file 1024M   0B   -2

We can check the output of the free utility again to corroborate our findings:

free -h
              total        used        free      shared  buff/cache   available
Mem:          990Mi        37Mi       860Mi       4.0Mi        92Mi       834Mi
Swap:         1.0Gi          0B       1.0Gi

Making the Swap File Permanent

Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our /etc/fstab file.

Back up the /etc/fstab file in case anything goes wrong:

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

Add the swap file information to the end of your /etc/fstab file by typing:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Next, we’ll review some settings we can update to tune our swap space.

The swappiness parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage.

With values close to zero, the kernel will not swap data to the disk unless absolutely necessary. Remember, interactions with the swap file are “expensive” in that they take a lot longer than interactions with RAM and they can cause a significant reduction in performance. Telling the system not to rely on the swap much will generally make your system faster.

Values that are closer to 100 will try to put more data into swap in an effort to keep more RAM space free. Depending on your applications’ memory profile or what you are using your server for, this might be better in some cases.

We can see the current swappiness value by typing:

cat /proc/sys/vm/swappiness

For a Desktop, a swappiness setting of 60 is not a bad value. For a server, you might want to move it closer to 0.

We can set the swappiness to a different value by using the sysctl command.

For instance, to set the swappiness to 10, we could type:

sudo sysctl vm.swappiness=10
vm.swappiness = 10

This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

At the bottom, you can add:


Press Ctrl X, Then Press Y and Lastly, the enter button to save the new configurations.

Adjusting the Cache Pressure Setting

Another related value that you might want to modify is the vfs_cache_pressure. This setting configures how much the system will choose to cache inode and dentry information over other data.

Basically, this is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it’s an excellent thing for your system to cache. You can see the current value by querying the proc filesystem again:

cat /proc/sys/vm/vfs_cache_pressure



As it is currently configured, our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:

sudo sysctl vm.vfs_cache_pressure=50


vm.vfs_cache_pressure = 50

Again, this is only valid for our current session. We can change that by adding it to our configuration file like we did with our swappiness setting:

sudo nano /etc/sysctl.conf

At the bottom, add the line that specifies your new value


Press Ctrl X, Then Press Y and Lastly, the enter button to save the new configurations.


You have now successfully created a swap file on your Debian 10 server and configured the system to utilize it efficiently. By adding swap space, you can improve your server’s performance and stability, especially when dealing with out-of-memory errors in applications. However, remember that using swap space on SSD storage is not recommended, as it can lead to hardware degradation over time.

Similar Posts