Linux OS

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.

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.

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.

or you may also attend online training: Learn Bash Shell in Linux for Beginners

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.

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.

Alaric Bird

Alaric Bird is a seasoned Linux System Administrator with over a decade of experience in managing and optimizing Linux-based servers and infrastructure. Known for his expertise in server deployment, security hardening, and performance tuning, Alaric has a deep understanding of various Linux distributions, including Ubuntu, CentOS, and Red Hat Enterprise Linux. His skills extend to cloud platforms like AWS, where he effectively manages virtual private servers and services. Alaric is also proficient in scripting languages such as Bash and Python, which he uses to automate routine tasks, enhancing efficiency and reliability. With a strong commitment to continuous learning, he stays updated with the latest developments in open-source technologies and best practices. His problem-solving abilities, combined with excellent communication skills, make him a valuable asset to any IT team. In addition to his technical expertise, Alaric is passionate about mentoring junior administrators and fostering a collaborative environment.

Recent Posts

Puppy Linux: Fast and Simple OS

Puppy Linux is a fast, lightweight OS designed for speed and simplicity, perfect for old…

1 day ago

Change Apache Document Root in Linux

Learn how to change Apache document root in Linux by following this step-by-step guide. Adjust…

2 weeks ago

How to Change Apache Port in Linux

Discover how to change Apache port in Linux easily. Follow our simple guide to modify…

2 weeks ago

How to Create Virtual Host in Apache Server

Learn how to create a virtual host in Apache Server with this comprehensive guide. Set…

3 weeks ago

10 Practical Tasks for RHCSA Exam with Solutions

Discover 10 practical tasks for the RHCSA exam with step-by-step solutions. Boost your Linux skills…

3 weeks ago

Ultimate Fail2ban Configuration Guide

Discover the ultimate Fail2ban configuration guide. Learn how to set up, customize, and optimize Fail2ban…

4 weeks ago

This website uses cookies.