Skip to content

How to Configure Swappiness to Use Swap Space Only When RAM is Full

Category
Linux
Covers
Swap
On this page

Linux systems use both RAM and swap space to manage memory. RAM is the physical memory, while swap is a space on the hard drive used when RAM is full. By default, Linux can start using swap even if RAM is not full. You can change this by adjusting the swappiness value.

How to Set Swappiness to Use Swap Only When RAM is Full

If you want your system to use swap only when RAM is almost full, you should set the swappiness value to 1.

Steps to Change the Swappiness Value

  1. Open a terminal.
  2. To temporarily set swappiness to 1, type this command:
  3. sudo sysctl vm.swappiness=1
    
  4. To make this change permanent, follow these steps:
    • Open the file /etc/sysctl.conf with a text editor:
    • sudo nano /etc/sysctl.conf
      
    • Add or modify this line in the file:
    • vm.swappiness=1
      
    • Save the file and exit the editor.
  5. To apply the change, run this command:
  6. sudo sysctl -p
    
Lower the Swap Priority over RAM
Lower the Swap Priority over RAM

Conclusion

By setting the swappiness value to 1, your Linux system will prioritize using RAM and only use swap space when the RAM is nearly full. This can help improve performance in most cases.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top