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 is divided 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 stores the mappings between virtual and physical addresses.
Virtual memory pages provide several advantages to memory management, including the swapping mechanism. When memory is low, inactive pages are moved to disk or secondary storage and swapped back into memory when they are 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 function call stacks and local variables. The pages operate under the last-in-first-out (LIFO) principle and are crucial for managing function execution and program state.
- 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 the virtual memory address space for a process. Changes to file-backed pages are reflected in 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 store 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 to useย swap spaceย when physical memory reaches its limit.
The swappiness value is a number between 0 and 200. Lower values indicate minor swapping, and higher values indicate aggressive swapping. When physical memory runs low, the swappiness value guides the kernel's decision to swap 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 how swap space is used. When processes are swapped out of RAM, they are moved to the swap space. The space is an extension of physical memory and helps free up RAM.

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 size of the swap space 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 benefit from frequent swapping should use higher swappiness values. The procedure ensures the RAM space is freed up frequently for active processes.
As swappiness determines the swapping ratio between file-backed and anonymous pages, it defines 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 instruct the kernel to be very aggressive in swapping out memory pages. |
Monitor system performance, memory usage, and incremental changes to determine the optimal swap value for the specific 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 a 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.



