Introduction

SWAP space is a partition and/or file located on a permanent storage media such as a hard drive or SSD that is treated as an extension of your system RAM. When your system is low on memory or when a particular portion of the memory has remained unchanged for some time, data may be moved from your "real" RAM into SWAP until it is needed again in order to free up your actual memory for more active or higher priority processes.

"Swappiness" therefore is a system parameter that plays a role indetermining how SWAP space is managed. Many distributions of GNU+Linux, including Debian (at least as of Bookworm) default to a swappiness value of 60, and can range from 0 to 100, with lower values corresponding to a decreased tendency to use SWAP.

The "SSD Optimization wiki page" has a section discussing swappiness as it pertains to prolonging the life of SSDs.

To view your current swapiness value you can use one of the two following commands:

cat /proc/sys/vm/swappiness

OR

/sbin/sysctl vm.swappiness

Note: If you run these commands as root you don't have to prepend /sbin/ to the 2nd command.

The ideal swappiness value for your system depends on your workload and your available system memory. If you feel the need or desire to make adjustments to this parameter, it's best to do so in small increments and observe the results. The impact of modifying swappiness is not as straight forward as it might seem, so you may want to do additional reading:

As of writing this wiki page, I cannot seem to find any "official" guidance in the Debian manual(s) regarding swappiness, presumably because they've already set a default value and deviating from that should be done on a case by case basis by system administrators. If you're not sure, especially if you're just using your computer as a normal desktop PC, just leave this setting at its default and you should be just fine.

But for me personally, here are the swappiness values I've been experimenting with:

Modify it On a Running System

You can modify your swappiness on a running system without restarting the system or running processes with the following steps.

  1. Modify the swappiness value
  2. Disable all currently in use SWAP space so that data currently in it is flushed to system RAM. (Optional)
  3. Re-enable all SWAP space so it's again available for use by the system using the new swappiness value. (Only required if you also did #2)

Use these commands as root or via sudo:

Replace FOO with your desired value. This command takes effect immediately.

sysctl vm.swappiness=FOO

This step is optional, but will disable SWAP and flush it to memory. This may take a moment if your SWAP is particularly full. Make sure that you have enough system RAM available to hold what is currently in RAM plus whatever is currently in SWAP. If your available system RAM is less than the amount of data currently living in SWAP this may cause issues, so committing the change to sysctl.conf and a full system reboot may be in order.

swapoff -a

This step re-enables SWAP and is only required if you also disabled SWAP in the previous step.

swapon -a

Make It Permanent

To modify your swappiness value permanently you need to either modify or add a value for the parameter in /etc/sysctl.conf . The following commands should be ran directly as root, or via sudo:

First, make a backup of your existing file:

cp /etc/sysctl.conf /etc/sysctl.conf.bak

Then edit the existing file:

nano /etc/sysctl.conf

First, verify that there isn't already an existing value for swappiness defined somewhere in this file. You can use "CTRL+W" to invoke the search function of nano and then search for "swappiness". If you've never modified this file before, you should get no results. Use "CTRL+C" to cancel the search function, then scroll all the way to the bottom of the file. On a new line, enter your desired value:

vm.swappiness=FOO

Where FOO is your desired value. Here's a screenshot of the end of my sysctl.conf showing my value and a comment I put above it to remind myself what this change was for.

swappiness.png

Then use CTRL+O to save the file, Enter to confirm the file name, then CTRL+X to exit nano.

Modifying this file however doesn't change the swappiness value currently in use. Your options to put the desired value into force are to either reboot the system or to follow the directions for temporarily modifying the value on a running system.


CategorySystemAdministration