In Ubuntu Linux, “swap” acts as emergency virtual memory. When your system starts running low on physical RAM, it moves less frequently used data from the RAM to a dedicated swap file or partition on your hard drive. This prevents your computer from crashing when memory runs out.
However, accessing data from a hard drive is significantly slower than accessing it from RAM. The kernel parameter that decides how aggressively your system moves data to swap is called “swappiness.” By default, Ubuntu’s swappiness is set to 60. Lowering this value forces your system to rely on physical RAM as much as possible, which can drastically improve responsiveness, especially on systems with 8GB of RAM or more.
In this guide, you will learn how to safely adjust the swappiness value in Ubuntu.
How to Check Your Current Swappiness
First, verify your system’s current configuration.
- Open your terminal emulator (Ctrl + Alt + T).
- Type the following command and press Enter:
cat /proc/sys/vm/swappiness
The terminal will likely output 60, which is the standard default for Ubuntu Desktop.
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 value of 10 is generally recommended for desktop users, as it tells the kernel to avoid swapping until RAM reaches 90% capacity.
- In the terminal, type the following command to temporarily change the value to 10:
sudo sysctl vm.swappiness=10 - Press Enter and type your administrator password when prompted.
This change takes effect immediately. Use your computer normally for a few hours. If everything feels smooth and responsive, you can proceed to make the change permanent.
How to Make the Swappiness Change Permanent
To ensure your new swappiness value survives a reboot, you must edit a core system configuration file.
- Open the terminal.
- Open the sysctl configuration file using the nano text editor with root privileges:
sudo nano /etc/sysctl.conf - Use the down arrow key to scroll to the very bottom of the file.
- On a new, blank line at the bottom, type the following exactly:
vm.swappiness=10 - Save the file by pressing Ctrl + O, then press Enter to confirm the filename.
- Exit the nano editor by pressing Ctrl + X.
The next time you reboot your Ubuntu machine, it will read this file and automatically apply the new swappiness value of 10, resulting in a snappier, more responsive desktop experience.