How to change Swappiness in Linux

Share on Social Media

Learn how to change swappiness in Linux to optimize system performance. Follow our step-by-step guide to adjust this kernel parameter and enhance your system’s memory management. #centlinux #linux #ubuntu

What is Swap Partition?

A swap partition is a designated area on a hard drive or SSD that is used as virtual memory by the Linux operating system. When the physical RAM (Random Access Memory) is fully utilized, the system can move inactive or less frequently used memory pages from RAM to the swap partition. This helps free up physical memory for more urgent tasks and processes.

Key points about swap partitions:

  1. Virtual Memory Extension: Swap partitions extend the amount of memory available to the system by using disk space, which is slower than RAM but can prevent system crashes when RAM is exhausted.
  2. Performance: While using swap space can slow down system performance compared to using RAM, it provides a buffer that allows applications to continue running instead of failing due to lack of memory.
  3. Configuration: Swap partitions are usually created during the installation of the Linux operating system, but they can also be added or modified later. The size of the swap partition can vary depending on the system’s requirements and available disk space.
  4. Swappiness Parameter: The swappiness value (a kernel parameter) controls how often the system moves data to the swap partition. Lower values reduce swap usage, while higher values increase it.
  5. Alternatives: In addition to swap partitions, Linux systems can also use swap files, which are files on the filesystem that serve the same purpose as a dedicated swap partition.

In summary, a swap partition is a useful feature in Linux that helps manage memory more effectively, especially on systems with limited RAM. It acts as a safety net to ensure the system remains functional under heavy memory loads.

Recommended Online Training: Learn Bash Shell in Linux for Beginners

745772 0021

What is Swappiness?

The Linux kernel’s swappiness setting defines how aggressively it will swap memory pages versus dropping pages from the page cache.

A higher value increases swap aggressiveness, while a lower value tells the Linux kernel to swap as little as possible to disk and favor RAM.

  • 0: The kernel will avoid swapping processes out of physical memory for as long as possible. This is suitable for systems where response time is critical, and there is enough RAM to hold most or all of the running processes.
  • 100: The kernel will swap processes out more aggressively, preferring to free up physical memory by moving inactive processes to swap space. This can be useful for systems with limited RAM or when running many background processes.

The swappiness range is from 0 to 100, and most Linux distributions have swappiness set to 60 by default.

Some database servers managed cache to use RAM, and is capable of managing what should be in RAM and what shouldn’t be. Allowing the Linux operating system to have too much control over what memory pages are in RAM is likely to lower your database performance. Therefore, it is recommended that swappiness should be set to 0.

How to change Swappiness in Linux
How to change Swappiness in Linux

Check Current Swappiness Setting

Check current setting of swappiness.

# cat /proc/sys/vm/swappiness
60

Change Swappiness in Linux

Set the swappiness setting to 0 for the running system.

# sysctl -w vm.swappiness=0
vm.swappiness = 0

Verify the current setting of swappiness again.

# cat /proc/sys/vm/swappiness
0

To make swappiness setting persistent, you have to add it in Kernel parameter file.

# echo 'vm.swappiness=0' >> /etc/sysctl.d/99-swappiness.conf

You have successfully change the swappiness for your Linux distribution.

If you are new to Linux and facing difficulty in working at Linux Bash prompt. We recommend that, you should read The Linux Command Line, 2nd Edition: A Complete Introduction by William Shotts.

Video Tutorial: How to Adjust Swappiness in Linux

YouTube player

Final Thoughts

Understanding and adjusting the swappiness value in Linux can significantly improve your system’s performance based on your specific needs and workloads. By fine-tuning this parameter, you can optimize memory management and ensure that your system runs efficiently. If you need personalized assistance or further guidance on adjusting swappiness or other Linux performance tweaks, I offer professional services on Fiverr. Check out my Fiverr gig for expert help in optimizing your Linux system.

Leave a Comment