Categories
Design

What is swappiness on linux (and how to change it)

The current values of the Linux kernel and its components are made accessible using a special interface — the /proc directory [5]. This is a virtual file system in which the single files are filled with values in real time. The values represent the actual state the Linux kernel is in. You can access the individual files in the /proc directory using the cat command as follows:

One of these kernel parameters is called vm.swappiness. It “controls the relative weight given to swapping out of runtime memory, as opposed to dropping memory pages from the system page cache” [6]. Starting with Linux kernel releases 2.6 this value was introduced. It is stored in the file /proc/sys/vm/swappiness .

The use of swap [6] was an essential part of using smaller UNIX machines in the early 1990s. It is still useful (like having a spare tire in your vehicle) when nasty memory leaks interfere with your work. The machine will slow down but in most cases will still be usable to finish its assigned task. Free software developers have been making great strides to reduce and eliminate program errors so before changing kernel parameters consider updating to a newer version of your application and related libraries first.

If you run numerous tasks, then the inactive tasks will be swapped out to disk, making better use of memory with your active tasks. Video editing and other large memory consuming applications often have recommended amounts of memory and disk space. If you have an older machine which cannot have a memory upgrade, then making more swap available might be a good temporary solution for you (see [6] on how to learn more about that).

The swapping can happen on a separate partition or on a swap file. The partition is faster and favored by many database applications. The file approach is more flexible (see the dphys-swapfile package in Debian GNU/Linux [7]). Having more than one physical device for swapping allows the Linux kernel to choose the device that is most rapidly available (lower latency).

vm.swappiness

The default value of vm.swappiness is 60 and represents the percentage of the free memory before activating swap. The lower the value, the less swapping is used and the more memory pages are kept in physical memory.

The value of 60 is a compromise that works well for modern desktop systems. A smaller value is a recommended option for a server system, instead. As the Red Hat Performance Tuning manual points out [8], a smaller swappiness value is recommended for database workloads. For example, for Oracle databases, Red Hat recommends a swappiness value of 10. In contrast, for MariaDB databases, it is recommended to set swappiness to a value of 1 [9].

Changing the value directly influences the performance of the Linux system. These values are defined:

* 0: swap is disable
* 1: minimum amount of swapping without disabling it entirely
* 10: recommended value to improve performance when sufficient memory exists in a system
* 100: aggressive swapping

As shown above the cat command helps to read the value. Also, the sysctl command gives you the same result:

Keep in mind that the sysctl command is only available to an administrative user. To set the value temporarily set the value in the /proc file system as follows:

As an alternative you may use the sysctl command as follows:

To set the value permanently, open the file /etc/sysctl.conf as an administrative user and add the following line:

Conclusion

More and more linux users are running virtual machines. Each one has its own kernel in addition to the hypervisor that actually controls the hardware. Virtual machines have virtual disks created for them, so changing the setting inside the virtual machine will have indeterminate results. Experiment first with changing the values of the hypervisor kernel, as it actually controls the hardware in your machine.

For older machines which can no longer be upgraded (already have maximum supported memory) you can consider placing a small solid state disk in the machine to use it as an additional swap device. This will obviously become a consumable as memory cells fail from lots of writes, but can extend the life of a machine for a year or more for very low cost. The lower latency and quick reads will give much better performance than swapping to an ordinary disk, giving intermediate results to RAM. This should allow you to use somewhat lower vm.swappiness values for optimal performance. You will have to experiment. SSD devices are changing rapidly.

If you have more than one swap device, consider making it a RAID device to stripe data across the available devices.

You can make changes in swappiness without rebooting the machine, a major advantage over other operating systems.

Try to include only the services you need for your business. This will reduce memory requirements, improve performance and keep everything simpler.

A final note: You will be adding load to your swap devices. You will want to monitor the temperatures of them. An overheated system will lower its CPU frequency and slow down.

Acknowledgements

The author would like to say a special thanks to Gerold Rupprecht and Zoleka Hatitongwe for their critical remarks and comments while preparing this article.

  • Search
  • Log In
  • Language

Log in to Your Red Hat Account

Your Red Hat account gives you access to your profile, preferences, and services, depending on your status.

If you are a new customer, register now for access to product evaluations and purchasing capabilities.

Need access to an account?

If your company has an existing Red Hat account, your organization administrator can grant you access.

Red Hat Account

  • Account Details
  • User Management
  • Account Maintenance
  • Account Team

Customer Portal

  • My Profile
  • Notifications
  • Help

For your security, if you’re on a public computer and have finished using your Red Hat services, please be sure to log out.

Select Your Language

  • English
  • 한국어
  • 日本語
  • 中文 (中国)

Environment

  • Red Hat Enterprise Linux 4
  • Red Hat Enterprise Linux 5

Issue

  • What does swappiness do and how does it affect swap_tendency?

Resolution

Note : The swapping algorithm is overhauled in Red Hat Enterprise Linux 6 and up to consider active and inactive memory for both file-backed and anonymous (non-file-backed) memory. While swappiness still exerts a considerable shift in behaviour on swap activity on a system, swappiness no longer uses the algorithm described below. As such, swap_tendency , distress , and mapped_ratio no longer exist in Red Hat Enterprise Linux 6 and above.

Swappiness is a property for the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100, inclusive. A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space.

As this parameter sets the kernel’s balance between reclaiming pages from the page cache and reclaiming pages by swapping out process memory, a discussion of page reclaim overall is warranted. The reclaim code works (in a very simplified way) by calculating a few numbers:

The distress value is a measure of how much trouble the kernel is having freeing memory. The first time the kernel decides it needs to start reclaiming pages, distress will be zero; if more attempts are required, that value goes up, approaching a max value of 100.

The mapped_ratio value is an approximate percentage of how much of the system’s total memory is mapped (i.e. is part of a process’s address space) within a given memory zone.

And vm_swappiness is the swappiness parameter, set to 60 by default.

With these numbers in hand, the kernel calculates its “swap tendency”:

If swap_tendency is below 100, the kernel will only reclaim page cache pages. Once it goes above that value, however, pages which are part of some process’s address space will also be considered for reclaim. So, if the system’s distress value is low and swappiness is set to 60, the system will not swap process memory until 80% of the total RAM in the system is allocated. Users who would like to never see application memory swapped out can set swappiness to a low value, say 5 or 10, causing the kernel to ignore process memory until the distress value gets quite high.

Overall, increasing this value will make the system more inclined to utilize swap space, leaving more memory free for caches. Decreasing this value will make the system less inclined to swap, and may improve application responsiveness.

Tuning vm.swappiness incorrectly may hurt performance or may have a different impact between light and heavy workloads. Changes to this parameter should be made in small increments and should be tested under the same conditions that the system normally operates.

  • Product(s)
  • Red Hat Enterprise Linux
  • Component
  • kernel
  • Category
  • Learn more
  • Tags
  • memory
  • rhel
  • rhel_4
  • rhel_5
  • swap

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

How to create an email account in Plesk?
How to increase the ulimit and file descriptors limit in linux.
  • linux
  • swap
  • swappiness

It is one of feature available in Linux operating system which is responsible degree to which the kernel prefers to swap memory to fulfil the system memory requirement. The parameter value varies from 0 to 100. In Linux system default value set to 60. A low value result kernel will try to avoid swapping unless there is almost RAM is full for process on system. The higher value would force kernel aggressively to moved out memory pages from the physical memory to swap memory.

If we set higher value then there may be negative impact on the system cause when system accessing the swap memory (which is part of the hard drive) then system operations being bit slow as compare to the RAM as RAM is faster than the hard drive. To read/write system directly on hard drive will take time so it’s best to avoid transfer active memory pages to swap memory aggressively.

To check the default value use command:

# cat /proc/sys/vm/swappiness

60

-To modify the parameter use command:

#echo 40 > /proc/sys/vm/swappiness

-To affect the changes use :

# sysctl -p

-Verify the parameter use :

# sysctl -a | grep swappiness

What is swappiness on linux (and how to change it)

Linux swappiness is the rate at which a Linux platform’s kernel moves pages into and out of active memory.

The Linux platform does not wait until all available memory is used before swapping pages to disk. Instead, it swaps pages based on how long they have been inactive. Increasing the swappiness value makes the Linux kernel move inactive memory pages to swap sooner than when the swappiness parameter is low.

Swappiness is defined by a tunable value called the swappiness parameter, which can be set on a scale of 0 to 100. Linux servers are typically set to a swappiness value of 60. Administrators will adjust Linux swappiness to account for high memory loads on a server or other factors related to workloads and available resources.

  • Getting the most out of RHEL 5
  • Better Linux performance via swap cache changes
  • Server problems? Adjust Linux memory swap
  • Mastering Linux memory usage
  • How to configure Linux swappiness

Related Terms

  • What is swappiness on linux (and how to change it)
How to prevent swap usage from hurting your NVMe devices

What is swappiness on linux (and how to change it)

What is swappiness on linux (and how to change it)

Manage your VM resources effectively with these tips

What is swappiness on linux (and how to change it)

What is swappiness on linux (and how to change it)

Optimize memory resources in your virtual environment

What is swappiness on linux (and how to change it)

What is swappiness on linux (and how to change it)

Obtain page swap support with the hypervisor swapping feature

What is swappiness on linux (and how to change it)

  • The Economic and Employee Benefits of a Flexible Working Environment –Citrix
  • Shaking Up Memory with Next-Generation Memory Fabric –Western Digital
  • See More
  • Unix-to-Linux Migrations Handbook –SearchDataCenter.com
  • Red Hat Enterprise Linux for SAP Solutions –Red Hat

When you create a group managed service account, it relieves some administrative duties and bolsters the security related to .

The tech company confirmed there were signs of exploitation in the wild for the vulnerabilities that affect Windows desktop and .

When you plug your System Center Configuration Manager into the Microsoft cloud with this feature, you add more flexibility to .

Nested VMs make it easy to expand VM capacity without additional hardware. Take a look at the process to avoid any major issues .

It takes time to plan, assess and set up a VM migration. Microsoft’s Azure Migrate gives you a central dashboard to efficiently .

The use of software-defined data centers alters business buying criteria, removes traditional IT team silos and requires .

Network connectivity can make or break a cloud deployment. Discover the basics of Azure network services with this list of key .

AI and cloud computing work hand in hand to improve upon various processes, but the combination of these technologies comes with .

AWS Outposts is a managed device that brings Amazon cloud services into your data center. Review its requirements, use cases and .

  • About Us
  • Meet The Editors
  • Contact Us
  • Advertisers
  • Business Partners
  • Media Kit
  • Corporate Site
  • Contributors
  • Reprints
  • Answers
  • Definitions
  • E-Products
  • Events
  • Features
  • Guides
  • Opinions
  • Photo Stories
  • Quizzes
  • Tips
  • Tutorials
  • Videos

Posted by Sahil Suri | Sep 20, 2018 | Basics | 0 |

What is swappiness on linux (and how to change it)

Introduction

In an earlier article, we demonstrated how we could calculate and report the amount of swap memory used by an individual process. In this article, we will explain a kernel parameter related to the use to swap memory. This kernel parameter is called swapiness. Before we talk about swapiness, we’ll briefly explain the concepts of swapping and paging.

An overview of memory management in Linux
A process normally runs on physical memory where the memory is divided into sets of pages. A page is a 4kb area of memory and is the basic unit of memory with which both kernel and CPU deal. This page size is tunable. Operating systems use virtual memory which is a memory management technique wherein RAM and swap memory are combined gives an application program the impression that it has contiguous working memory. This memory management implementation allows processes requiring a large amount of memory of fork even if the required amount of RAM is not available at that point in time.

Swap space:
This is storage space allocated on a portion of or the entire hard disk which is used by the operating system to store data pages that are currently not needed. This swap space can be a partition as well as swap file. Although allocating swap space using swap files is not recommended.

Swapping and Paging
To swap a process means to move that entire process out of main memory and to the swap area on hard disk, whereby all pages of that process are moved at the same time. With paging only the least recently used pages of processes are moved to the swap space. Linux is a pure paging system it never swaps, neither under normal usage nor does it employ desperation swapping under heavy usage.

What is swapiness?
After a brief overview of swapping and paging we’ll now explain the concept of swapiness. Swapiness is a feature specific to Linux based operating systems that controls the amount of swap the kernel will use in an effort to free up physical memory. The value of swapiness is controlled by a tunable kernel parameter called vm.swappiness. It’s value can be set within a range of 0 to 100. A low value of the vm.swapiness tunable implies that the kernel will avoid using swap space as much as possible and will make use of swap space only in extreme cases when there is almost no free RAM available to be allocated to any new process. A high value of swapiness indicates that the kernel would make vigorous use of swap space and will frequently swap out least recently used memory pages.

The default value for swapiness on RHEL based systems is 60. Using a higher value of swapiness will generally have an adverse impact of the performance of the system as the kernel will be moving pages in and out of swap more frequently. Frequent access of memory pages from hard disk based memory (swap space) will cause a decline in application performance and therefore using a higher value of swapiness should be avoided. However processes systems running programs that sleep over long intervals of time could benefit from a higher value of swapiness.

Check current value of swapiness:
To display the current value of swapiness we could type sysctl followed by the vm.swapiness parameter as shown below:

As we mentioned earlier, presently the value is set to default which is 60. We could also obtain the value of swapiness from the /proc file system by quering the /proc/sys/vm/swappiness file.

This gives the same result as the sysctl command.

Change current value of swapiness:
We could change the value of swapiness by updating the /proc/sys/vm/swappiness file with the new value followed by executing the sysctl command with the -p option to make the changes permanent. Given below is a demonstration.

Conclusion

In this article we briefly explained virtual memory management in Linux and how the swapiness value affects system performance. We hope that you’ve found this article to be useful and we look forward towards your feedback and suggestions.

(Farzand Ali)

The Linux kernel provides a tweakable setting that controls how often the swap file is used, called swappiness

A swappiness setting of zero means that the disk will be avoided unless absolutely necessary (you run out of memory), while a swappiness setting of 100 means that programs will be swapped to disk almost instantly.

Ubuntu system comes with a default of 60, meaning that the swap file will be used fairly often if the memory usage is around half of my RAM. You can check your own system’s swappiness value by running:

As I have 4 GB of RAM, so I’d like to turn that down to 10 or 15. The swap file will then only be used when my RAM usage is around 80 or 90 percent. To change the system swappiness value, open /etc/sysctl.conf as root. Then, change or add this line to the file:

Reboot for the change to take effect

You can also change the value while your system is still running

you can also clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.

To calculate your swap Formula

so total is 3950 / 100 = 39.5 * 10 = 395

so what it mean is that when 10 % 395 MB of ram left then it start using swapiness

Help . Ubuntu . Swap

What is swappiness

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

. swappiness can have a value of between 0 and 100

. swappiness=0 : Version 3.5 and over: disables swapiness. Prior to 3.5: tells the kernel to avoid swapping processes out of physical memory for as long as possible.

. swappiness=1 : Version 3.5 and over: Minimum swappiness without disabling it entirely

. swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache

The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation. A value of swappiness=10 is recommended, but feel free to experiment.

Started Using swap at 91%

What is swappiness on linux (and how to change it)As I have configured my system & vm to make use of ram at 90 % . At 90 % there was no swapping

after that I opened some application like Firefox & Shutter , its start swapping because of ram usage is above 90 %

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. What is the default setting and how to configure that to improve overall performance?

What is swappiness on linux (and how to change it)

3 Answers 3

The Linux kernel provides a tweakable setting that controls swappiness

open /etc/sysctl.conf as root. Then, change or add this line to the file:

for changing the swappiness value temporarily try this command:

What is swappiness on linux (and how to change it)

What is swappiness on linux (and how to change it)

You could set this value in a virtual file /proc/sys/vm/swappiness The mentioned value will be deciding how the swap space should be used, below are the values with their intents.

Although it actually depends upon the need too, if the physical memory available is sufficient, there may not be a great need of swap space, in Layman’s terms there won’t be a need of changing the default value of 60 .

In RedHat/CentOS the default value is 60.
“In order to improve performance” is very broad term. What performance are you trying to improve?

Do you have issues with low memory?
Does your system SWAP when there’s still free memory/cached memory?

In Linux free RAM = wasted RAM, so almost all free memory is used for disc caches.
There are cases with swappiness=60 where pages in memory are moved to SWAP if they haven’t been accessed for extended time periods, no matter you have unallocated RAM.
Moving some memory pages to SWAP is not necessary a bad thing.

Please shed some more light on your issue for more detailed answer.

Linux kernel has improved memory subsystem, with which administrators now have a simple interface to fine-tune the swapping behavior of the kernel. The linux kernel tunable parameter vm.swappiness (/proc/sys/vm/swappiness) can be used to define how aggressively memory pages are swapped to disk.

vm.swappiness

Linux moves memory pages that have not been accessed for some time to the swap space even if there is enough free memory available. By changing the percentage in /proc/sys/vm/swappiness you can control the swapping behavior, depending on the system configuration.

High Swappiness Value?

A high swappiness value means that the kernel will be more apt to unmap mapped pages. A low swappiness value means the opposite, the kernel will be less apt to unmap mapped pages. In other words, the higher the vm.swappiness value, the more the system will swap.

vm.swappiness takes a value between 0 and 100 to change the balance between swapping applications and freeing cache. At 100, the kernel will always prefer to find inactive pages and swap them out; in other cases, whether a swapout occurs depends on how much application memory is in use and how poorly the cache is doing at finding and releasing inactive items.

Systems with memory constraints that run batch jobs (processes that sleeps for long time) might benefit from an aggressive swapping behavior.

Changing Swapping Behavior

To change swapping behavior, use either echo or sysctl

Tuning the Linux memory subsystem is a tough task that requires constant monitoring to ensure that changes do not negatively affect other components in the server. If you do choose to modify the virtual memory parameters (in /proc/sys/vm), change only one parameter at a time and monitor how the server performs.

I am a bit confused because everyone says to reduce the swappiness to about 10; but if I have only 4GB RAM than how much swappiness would be the best? and also If I had 8 and 16GB ram?

and also I am not talking about swap partition/file size I am talking about swappiness.

[Edit]: I don’t want to know that what swappiness does, instead I want how much I need. According to a suggestion below I am listing some common tasks and apps I use. I am a programmer so apps like: VScode, Pycharm are neccesary for me and also some heavy tasks like virtualization (most of the times of android or windows) are in my usual work. And I also do video editing and graphics design, but, for those I use light programs (I think they are because they feels so fast) like: Shotcut, Inkscape.
So, I think this info would help in answering the question

Will appreciate an answer!:))

What is swappiness on linux (and how to change it)

2 Answers 2

Set it to 10 you probably won’t even notice a difference during normal use cases maybe when your compiling but you can always use sysctl vm.swappiness=X and try out a setting for a little bit while your working most people are not going to notice a major difference in performance. If you concerned about SSD life check out one of the many posts and articles about it basically it “may” have been an issue at one point in time but that time has come and gone when your dealing with even a marginally good drive. The predicted life for a 128 GB MLC SSD of medium quality (3000 write cycles before failure) and average of 10GB write per day is around 35000 days so we are dealing with absurd times here. Don’t over think the issue.

Just to bring it up in case your interested in this stuff look into trim for SSD’s. Can start a whole new interesting discussion.

What is swappiness on linux (and how to change it)

You’re starting things with a wrong assumption: Swappiness don’t change with how much RAM you have, but with what software you’ll use.

If you do virtual machine, you don’t want a high swappiness. But if you do graphics design or coding, swappiness may be high.

But as a rule of thumbs: Set your swappiness to 0, so Linux will swap as late as it can. Doing this will keep your system responsive, and prolong your SDD lifespan if you have one (that’s why you mostly saw answers to set swappiness to 10 and nothing else).

As others say, swap will only be an emergency “RAM” for most people.

Also, considering that you only have 4 GBs of RAM, you’ll pretty quickly reach that limit in no time just with web browsing alone, swap won’t even help here. Even 8GBs of RAM won’t be enough for what you intend to do.

Then, depending on what your CPU is, you may still have a hard time even with more RAM. To make it short: there is a relation between your CPU and how much RAM you should have, but also what you intend to do (which you told us already).

Some tips about swap:

  • Swap partitions are useless for most people, do use swap files instead.
  • Swap should be at the very least the physical size of your RAM (not how much the OS detects them) PLUS the amount your dedicated GPU(s) physically has, if you have one. That’s even truer if you do make use of hibernation.

Also: Just because a program looks “fast”, doesn’t mean it’s lightweight since caching is a thing that is overly used since less than a decade. In short: You’ll trade less CPU usage for far more RAM usage.