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.
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:
[root@linuxnix ~]# sysctl vm.swappiness vm.swappiness = 60
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.
[root@linuxnix ~]# cat /proc/sys/vm/swappiness 60 [root@linuxnix ~]#
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.
[root@linuxnix ~]# echo 25 > /proc/sys/vm/swappiness [root@linuxnix ~]# [root@linuxnix ~]# sysctl -p net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296 kernel.sem = 250 32000 100 128 fs.file-max = 6815744 [root@linuxnix ~]# [root@linuxnix ~]# sysctl -a | grep vm.swappiness vm.swappiness = 25 [root@linuxnix ~]#
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.
Latest posts by Sahil Suri (see all)
- RHEL 8 installation step by step with screenshots - August 19, 2019
- 3 ways to obtain per process swap utilization in Linux - August 16, 2019
- Docker engine explained - August 15, 2019
- Understanding Linux system logging - August 14, 2019
- Docker architecture explained - August 12, 2019