How to Change the Swappiness Value in Ubuntu Linux

In Ubuntu Linux, “swap” acts as emergency virtual memory. When your system starts running low on physical RAM, the kernel moves less frequently used data from RAM to a dedicated swap file or partition on your storage drive. This prevents applications from crashing when memory runs out, but there is a significant trade-off: accessing data from a hard drive or even an SSD is considerably slower than accessing it from RAM.

The kernel parameter that controls how aggressively your system moves data to swap is called “swappiness.” This value ranges from 0 to 200 (on modern kernels), with the default set to 60 on Ubuntu Desktop. A higher value means the kernel will swap data more readily, whilst a lower value forces it to rely on physical RAM as much as possible. Adjusting this parameter can make a noticeable difference to desktop responsiveness, particularly on systems with 8 GB of RAM or more where excessive swapping is rarely necessary.

What the Swappiness Value Actually Means

The swappiness value does not represent a percentage of RAM usage that triggers swapping. Instead, it is a weighting factor that tells the kernel how to balance between reclaiming memory from the page cache versus swapping out anonymous memory (application data).

  • vm.swappiness = 0 — The kernel avoids swapping anonymous pages almost entirely. It will only swap when the system is critically low on memory. This is generally too aggressive for most desktop users, as it can lead to out-of-memory situations.
  • vm.swappiness = 10 — A commonly recommended value for desktop systems. The kernel strongly prefers keeping application data in RAM and only swaps when memory pressure becomes significant.
  • vm.swappiness = 60 — The Ubuntu default. The kernel balances between reclaiming page cache and swapping. This works well for general-purpose servers but can cause sluggishness on desktops with plenty of RAM.
  • vm.swappiness = 100 — The kernel treats file-backed and anonymous pages equally, leading to more aggressive swapping.

For most Ubuntu desktop users with 8 GB of RAM or more, setting swappiness to 10 provides the best balance between performance and stability.

How to Check Your Current Swappiness Value

Before making any changes, verify your system’s current configuration.

  1. Open your terminal emulator (Ctrl + Alt + T).
  2. Type the following command and press Enter:
    cat /proc/sys/vm/swappiness

The terminal will output a number. On a fresh Ubuntu Desktop installation, this will typically be 60.

You can also check using the sysctl command:

sysctl vm.swappiness

This will display vm.swappiness = 60 (or whatever the current value is).

How to Monitor Your Current Swap Usage

Before changing the swappiness value, it is worth understanding how much swap your system is actually using. This helps you determine whether adjusting the value will make a practical difference.

  1. In the terminal, type:
    free -h
  2. Look at the “Swap” row in the output. It will show total swap space, how much is currently used, and how much is free.

If you notice several hundred megabytes or even gigabytes of swap in use whilst you still have free RAM available, your swappiness value is likely too high for your use case.

For a more detailed view, you can check which processes are using swap:

grep VmSwap /proc/*/status 2>/dev/null | sort -k2 -n -r | head -10

This command lists the top 10 processes consuming swap space, helping you identify whether specific applications are being swapped unnecessarily.

How to Test a New Swappiness Value Temporarily

Before making a permanent change, it is wise to test a new value to ensure your system remains stable. A temporary change allows you to evaluate the impact without risking your system’s configuration.

  1. In the terminal, type the following command to change the value to 10:
    sudo sysctl vm.swappiness=10
  2. Press Enter and type your administrator password when prompted.
  3. Verify the change took effect:
    cat /proc/sys/vm/swappiness

The output should now display 10. This change takes effect immediately but will revert to the default value (60) when you restart your computer. Use your system normally for several hours or even a full day before committing to a permanent change.

How to Make the Swappiness Change Permanent

Once you are satisfied with the temporary change, you can make it survive reboots by editing a core system configuration file.

  1. Open the terminal (Ctrl + Alt + T).
  2. Open the sysctl configuration file using nano with root privileges:
    sudo nano /etc/sysctl.conf
  3. Use the down arrow key to scroll to the very bottom of the file.
  4. On a new, blank line at the bottom, type the following exactly:
    vm.swappiness=10
  5. Save the file by pressing Ctrl + O, then press Enter to confirm.
  6. Exit nano by pressing Ctrl + X.

To apply the change immediately without rebooting, run:

sudo sysctl -p

This command reloads all settings from /etc/sysctl.conf, including your new swappiness value.

Recommended Swappiness Values for Different Use Cases

Use Case Recommended Value Reasoning
Desktop with 8 GB+ RAM 10 Keeps applications responsive by minimising unnecessary swapping
Desktop with 4 GB RAM 30–40 Allows moderate swapping to prevent out-of-memory situations
Web server 10–20 Prioritises keeping frequently accessed data in RAM
Database server 1–10 Databases manage their own caching and benefit from minimal swapping
Systems with SSD storage 10–20 Reduces unnecessary write operations that can reduce SSD lifespan

Common Mistakes to Avoid

  • Setting swappiness to 0 on a desktop: Whilst this might seem logical, a value of 0 does not disable swap entirely. It simply tells the kernel to avoid swapping until absolutely necessary, which can lead to the out-of-memory killer terminating important processes unexpectedly.
  • Removing swap entirely: Even with abundant RAM, having swap space provides a safety net. Applications with memory leaks or unexpected spikes in usage can crash your entire system if no swap is available.
  • Not monitoring after the change: After adjusting swappiness, monitor your system for a few days using free -h and system monitoring tools to ensure performance has genuinely improved.

How to Revert to the Default Value

If you experience any instability or want to return to Ubuntu’s default behaviour:

  1. Open the terminal and edit the sysctl configuration file:
    sudo nano /etc/sysctl.conf
  2. Find the line vm.swappiness=10 and either delete it entirely or change the value back to 60.
  3. Save and exit (Ctrl + O, Enter, Ctrl + X).
  4. Apply the change immediately:
    sudo sysctl -p

Your system will now behave exactly as it did before the modification.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.