Introduction
Swappiness is a Linux kernel parameter commonly linked to enhancing system performance. However, the parameter does not directly improve performance, and a high swappiness value can have unfavorable outcomes. Change advanced system parameters with extra caution.
This article provides an in-depth explanation of swappiness. You will learn how swapping works and how to adjust it on a Linux system.
Intro to Memory Mapping
Memory mapping is a critical concept that helps provide insight into the role of swappiness. The memory mapping mechanism connects logical (virtual) and physical memory addresses.
The first step is to define the distinction between physical and virtual memory.
- Physical memory. The physical memory of a machine refers to the RAM. It represents the physical hardware where data and instructions reside during program execution.
- Virtual memory. The virtual memory of a computer is an abstraction the operating system provides for every running program. Virtual memory has an address space independent of the physical memory.
Programs interact with virtual memory addresses, which the operating system maps to physical addresses.
Memory Pages
The virtual memory divides into pages, each with a mapped physical address. Pages are fixed-size blocks and the basic units for memory allocation, addressing, and management. Every page has a unique address, and the page table data structure stores the maps between virtual and physical addresses.
Virtual memory pages provide several advantages to memory management, including the swapping mechanism. When memory is low, inactive pages move to disk or secondary storage and swap back into memory when activated again.
Types of Memory Pages
Different memory pages serve specific purposes. Every page type utilizes different data types and operations. Common memory page types include:
- Code pages. The pages store executable instructions of programs. Code pages contain machine code and have read-only permissions to prevent accidental changes. Processes that execute the same programs share code pages to optimize memory use.
- Data pages. They hold non-executable data for programs, such as variables, constants, or data structures. Data pages are writeable, and programs freely modify the values when required. Processes do not share data pages.
- Stack pages. Stack pages are dynamic and store stacks for function calls and local variables. The pages operate using the last-in-first-out (LIFO) principle and are crucial in managing function executions and program states.
- Heap pages. The pages store dynamic memory allocation and deallocation from functions such as
malloc()
andfree()
. Heap pages store data with a predetermined size and lifetime. The data requires deallocation to prevent memory leaks. - File-backed pages. These pages are directly associated with specific files on the disk. They are crucial in memory-mapped files and directly connect virtual memory address space for a process. Changes to file-backed pages reflect on the original file, providing a convenient way to read and write file data.
- Anonymous pages. Also known as private pages, anonymous pages are not associated with any particular files and are not tied to persistent storage. These pages hold data for dynamically allocated memory, such as variables and data created at runtime.
- Shared memory pages. Shared memory pages hold shared data between multiple processes. The pages enable direct reading and writing from shared memory and enhance process synchronization.
Different page types contribute to efficient memory management in computers. As a memory management parameter, swappiness balances the swapping ratio between file-backed pages and anonymous pages.
Note: Learn the differences between Stack and Heap.
What Is Swappiness?
Swappiness is a Linux kernel parameter that controls how assertive the swapping mechanism is. Swapping moves inactive memory pages from physical memory to swap memory on a disk (either in a swap file or a swap partition). The process aims to balance data in RAM and utilizes the swap space when physical memory reaches its limits.
The swappiness value is a number between 0 and 200. Lower values indicate minor swapping, and higher values aggressive swapping. When physical memory runs low, the swappiness value directs the kernel's decision for swapping pages.
Ideal candidates for swapping are inactive or infrequently accessed pages. The process frees up RAM space, prioritizing more active processes.
The Relationship between Swappiness & Swap
The swappiness parameter affects how swapping occurs and the utilization of swap space. When processes swap out of RAM, they move to the swap space. The space is an extension of physical memory and aids in freeing up RAM space.
Swap space has a specific size and determines the amount of data a system can swap out. Once processes in RAM are complete and free up memory, the swapped processes return to RAM. Swappiness does not control the swap space size but instead influences the swapping limits and frequency.
Note: Learn the difference between Swap Partition and Swap File.
What Is the Best Swappiness Value?
Choosing the best swappiness value depends on many different factors, such as:
- System configuration.
- Workload.
- Memory.
- Performance requirements.
Lower swappiness values prioritize keeping data in RAM. Choose a lower value for systems that have abundant physical memory. Minimizing swapping frequency reduces the frequency of disk I/O operations. In this case, a lower swappiness value is beneficial.
On the other hand, higher swappiness values result in assertive swapping. Systems with limited physical memory and workloads that find frequent swapping advantageous benefit from higher swappiness values. The procedure ensures the RAM space frees up frequently for active processes.
As swappiness determines the swapping ratio between file-backed and anonymous pages, the number determines a tradeoff between the two. When swappiness is maximum, anonymous and file-backed pages have equal swapping priorities, whereas when swappiness is 0, the file-backed pages gain priority.
The table below provides examples of swappiness values and their effect:
Swappiness Value | Swappiness Effect |
---|---|
0 | The value instructs the kernel to avoid swappiness as much as possible. |
10-50 | The value tells the kernel to be slightly aggressive in swapping out memory pages. |
50-100 | The value instructs the kernel to be moderately aggressive in swapping out memory pages. |
>100 | The higher values instructs the kernel to be very aggressive in swapping out memory pages. |
Monitor the system performance, memory use, and incremental changes to determine the best swap value for the particular setting. Changing swappiness does not require a system restart.
How to Check Swappiness Value
There are two ways to check the swappiness value on a Linux system. Both methods require access to the terminal and provide the same information about the current swappiness value.
Option 1: Checking the /proc/sys/vm/swappiness File
Use the cat command to check the contents of the /proc/sys/vm/swappiness file. Enter the following in the terminal:
cat /proc/sys/vm/swappiness
The output shows a number between 0 and 200.
Option 2: Checking the sysctl Parameter
Alternatively, use the sysctl command to check the vm.swappiness
parameter value:
sysctl vm.swappiness
The output shows vm.swappiness=[value]
, where [value]
is a number between 0 and 200.
How to Change Swappiness Value
Changing the swappiness value on a Linux system requires administrator privileges. The steps differ based on whether the change is temporary or the value persists after restart.
To temporarily change swappiness, set vm.swappiness
to the desired value with the following:
sudo sysctl vm.swappiness=[value]
Alternatively, to have the changes persist after restart, open the /etc/sysctl.conf file using a text editor:
sudo nano /etc/sysctl.conf
Append the value as follows:
vm.swappiness = [value]
Save the file and close the editor to commit the changes.
Conclusion
After reading this guide, you know what the swappiness value is, how it works, and how to adjust it. Fine-tuning swappiness in Linux can improve system performance. However, the task is not straightforward and requires trial and error.
Next, see how to monitor memory usage in Linux.