What is Swappiness?

Most Linux users that have installed a distribution before, must have noticed the existence of the “swap space” during the partitioning phase (it is often found as /sda5). This is a dedicated space in your hard drive that is usually set to at least twice the capacity of your RAM, and it constitutes the total virtual memory of your system. The swap space can also be provided by a so-called swap-file, which you can use in case you can not create a dedicated swap partition. In general, swap partitions are preferred over swap files speed wise though. From time to time, the Linux kernel utilizes this swap space by copying chunks from your RAM to the swap, allowing active processes that require more memory than it is physically available to run.

Swappiness is the kernel parameter that defines how much (and how often) your Linux kernel will copy RAM contents to swap. This parameter's default value is “60” and it can take anything from “0” to “100”. The higher the value of the swappiness parameter, the more aggressively your kernel will swap.

Why change it?

The default value is a one-fit-all solution that can't possibly be equally efficient in all of the individual use cases, hardware specifications and user needs. Moreover, the swappiness of a system is a primary factor that determines the overall functionality and speed performance of an OS. That said, it is very important to understand how swappiness works and how the various configurations of this element could improve the operation of your system and thus your everyday user experience.

As RAM memory is so much larger and cheaper than it used to be in the past, there are many users nowadays that have enough memory to almost never need to use the swap file. The obvious benefit that derives from this is that no system resources are ever occupied by the swapping process and that cached files are not moved back and forth from the RAM to the swap and vise Versa for no reason.

How to change Swappiness?

The swappiness parameter value is stored in a simple configuration text file located in /proc/sys/vm and is named “swappiness”. If you navigate there through the file manager, you will be able to locate the file and open it to check your system's swappiness. You can also check it or change it through the terminal (which is faster) by typing the following command:

sudo sysctl vm.swappiness=10

or whatever else between “0” and “100” instead of the value “10” that I used. To ensure that the swappiness value was correctly changed to the desired one, you simply type:

cat /proc/sys/vm/swappiness

on the terminal again and the active value will be outputted.

Linux Swappiness

This change has an immediate effect on your system's operation and thus no rebooting is required. In fact, rebooting will revert the swappiness back to its default value (60). If you have thoroughly tested your desired swapping value and you found that it works reliably, you can make the change permanent by navigating to /etc/sysctl.conf which is yet another text configuration file. You may open this as root (administrator) and add the following line on the bottom to determine the swappiness: vm.swappiness="your desire value here". Then, save the text file and you're done!

Set swappiness value in Linux

Factors for consideration

There are some maths involved in the swappiness that should be considered when changing your settings. The parameter value set to “60” means that your kernel will swap when RAM reaches 40% capacity. Setting it to “100” means that your kernel will try to swap everything. Setting it to 10 (like I did on this tutorial) means that swap will be used when RAM is 90% full, so if you have enough RAM memory, this could be a safe option that would easily improve the performance of your system.

Some users though want the full cake and that means that they set swapping to “1” or even “0”. “1” is the minimum possible “active swapping” setting while “0” means disable swapping completely and only revert to when RAM is completely filled. While these settings can still theoretically work, testing it in low-spec systems of 2GB RAM or less may cause freezes and make the OS completely unresponsive. Generally, finding out what the golden means between overall system performance and response latency requires quite some experimentation (as always).

Frequently Asked Questions

What is Swap Space in Linux?

Swap space in Linux is a portion of the hard drive that is used when the RAM (Random Access Memory) is fully utilized. When the system requires more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. This process provides additional memory resources to the system at the cost of some performance, as accessing the hard drive is slower than accessing the RAM.

What is Swappiness in Linux?

Swappiness is a Linux kernel parameter that controls the degree to which the system favors swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can have a value between 0 and 100; a lower value means less swapping is favored, while a higher value means more swapping is favored.

How Can I Check the Current Swappiness Value?

You can check the current swappiness value by running the command cat /proc/sys/vm/swappiness in the terminal. This will display the current swappiness value, which is typically set to 60 by default in many Linux distributions.

How Do I Change the Swappiness Value?

To temporarily change the swappiness value, use the command sudo sysctl vm.swappiness=<value>, replacing <value> with your desired swappiness level. To make the change permanent, add vm.swappiness = <value> to your /etc/sysctl.conf file and then run sudo sysctl -p.

Does Increasing Swappiness Improve Performance?

Not necessarily. Increasing swappiness can lead to more frequent use of the swap space, which can slow down the system since accessing data on the hard drive is slower than in RAM. However, it can help in situations where memory is consistently full and programs are getting killed. It's a balance between keeping more applications in memory and the performance cost of using swap space.

Is It a Good Idea to Have a Swap Space Larger Than RAM?

The size of the swap space depends on your system's workload and configuration. In the past, the rule of thumb was to have a swap space twice the size of RAM. However, with modern systems having large amounts of RAM, this is often unnecessary. It's more important to monitor and understand your system's memory usage and adjust accordingly.

Can I Run Linux Without Swap Space?

Yes, it is possible to run Linux without swap space, especially on systems with large amounts of RAM. However, having no swap space can lead to issues when RAM is exhausted, such as applications being killed or system instability.

How Does Swappiness Affect SSDs (Solid State Drives)?

Swappiness can be an important factor for systems using SSDs, as frequent writes to swap space can potentially reduce the lifespan of an SSD due to its write endurance limits. However, modern SSDs are generally more durable, and the actual impact may vary depending on the SSD's quality and workload.

Can I Change Swap Space Size on a Running System?

You can adjust the size of the swap space on a running system. You can either create a new swap file or resize an existing one, then activate or deactivate it using the swapon and swapoff commands. Always ensure that resizing swap does not interfere with system stability.

How Do I Monitor Swap Usage on My System?

To monitor swap usage, you can use commands like free -m to see memory and swap usage, vmstat for detailed virtual memory statistics, and top or htop to monitor real-time memory and swap usage along with other system metrics.

Share this page:

21 Comment(s)