Signup/Sign In
LAST UPDATED: APRIL 3, 2023

How to Change the Swappiness Value in Linux

Technology #ubuntu#howto#linux

    How to Change the Swappiness Value  in Linux

    Memory is a vital aspect of every system. The Linux kernel recognizes this and works to guarantee there’s enough RAM at the system’s disposal. The Kernel maintains memory by employing a swap mechanism to produce more memory space.

    In this article, we will talk about what a swap is, how it works, and lastly, how to tune the swappiness of your Linux system.

    What Is Swap?

    A Swap system is described as space allocated on the disk by the operating system and utilized when RAM is inadequate. In Linux, the Swap system may be a partitioned disk area that operates as a swap or a swap file. The swap system may also mix both swap partition and swap files.

    When a system is out of physical memory, the Kernel swaps inactive pages from the main memory into the swap system. That frees up some space in the main memory for usage by other programs.

    Although you may get away without a swap system, if you have a system with less than 4 GB of RAM or a system with essential performance needs, a swap can come in extremely handy—and is recommended. Otherwise, if your system demands more memory than the system can offer, the system can crash, which might have fatal effects.

    Note: Do not think that a swap system is a substitute or an alternative to extra memory. A Swap system is nothing more than a partition or a file saved on the disk; it cannot wholly act as physical memory. If your system is continually running out of adequate memory, try adding extra physical memory since raising your swap system size won’t help much.

    What is Swappiness?

    Swappiness is a kernel setting that enables system administrators to change how frequently the system employs the swap system. Swappiness establishes the connection between switching out inactive pages from the RAM to the swap system.

    The swappiness feature was initially debuted in Linux kernel version 2.6; it spans from 0 to 100. The greater the swappiness number, the more the inactive pages are switched from the main memory to the swap area. Lower values will lead to static pages staying in the main memory.

    How to Change the Swappiness Value

    The default swappiness setting is typically the fair value for most use scenarios. However, you may wish to modify this number for numerous reasons.

    There is no ideal or suggested swappiness value. However, you may use several programs like free to monitor how your system memory operates and alter the swappiness setting until you discover your system’s best number.

    We may alter the swappiness value by changing the configuration file. This approach keeps the swappiness value even after a reboot.

    To achieve this, open the file /etc/sysctl.conf using your text editor and modify the value of the following item vm.swappiness to your desired value. (Add the entry if it does not exist).

    How to Change the Swappiness Value

    To adjust your system’s swappiness setting during runtime, use the sysctl command as shown: Change the swappiness value to meet your best deal.

    sudo sysctl vm.swappiness=10

    How to Change the Swappiness Value

    Note: Accessing swap space is substantially slower than accessing physical memory. Therefore, raising your swappiness value to 100 will not ensure a boost in performance.

    Conclusion

    In this article, we learned that the Linux kernel employs a swap mechanism to manage memory and ensure that there's enough RAM available for system usage. Swappiness, a kernel setting that determines how frequently the system uses the swap system, can be adjusted to optimise performance. However, it's important to note that a swap system is not a substitute for physical memory, and increasing the swappiness value too much can decrease performance.

    That's all for this article. Do share your experience with us!

    Frequently Asked Questions

    1. What is the swappiness value in Linux, and how does it affect performance?

    Swappiness vale in Linux is a kernel parameter that controls how often the Linux kernel swaps data between RAM and swap space. It affects the performance as High values = more swapping = more responsive but slower. Low values = less swapping = faster but less responsive when memory is low.

    2. How can I check the current swappiness value in Linux?

    You can check the current swappiness value in Linux by opening a terminal and running the command "cat /proc/sys/vm/swappiness". The current swappiness value is displayed, typically ranging from 0 to 100.

    3. How can I change the swappiness value in Linux?

    You can change the swappiness value in Linux by following these steps:

    • Open a terminal and run the command "sudo sysctl vm.swappiness=<value>". Replace "<value>" with the desired swappiness value, typically from 0 to 100.
    • To make the change persistent across reboots, open the "/etc/sysctl.conf" file with a text editor and add the line "vm.swappiness=<value>" to the end of the file.

    4. What swappiness value should I use in Linux?

    The swappiness value you should use in Linux depends on the amount of available RAM and the system's specific usage scenario. Generally, a swappiness value of 60 or lower is recommended for systems with sufficient RAM (8GB or more). A higher value may be more appropriate for systems with limited RAM (4GB or less).

    Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS