Site icon CentLinux

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.

How to change Swappiness in Linux

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.

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.

When to Adjust Swappiness:

Considerations:

In conclusion, tuning swappiness can be an important part of optimizing Linux system performance, especially in environments with varying memory needs. Adjusting the swappiness value based on the type of workload and available memory can help ensure that the system operates efficiently without over-relying on swap space.

How to change Swappiness in Linux

Check Current Swappiness Setting

Verify the current swappiness value configured on your Linux system. Swappiness determines how aggressively the kernel will swap memory pages between RAM and the swap space, influencing overall system performance. Checking the current setting is essential before making any adjustments, as it allows you to understand how your system is managing memory usage.

Swappiness values typically range from 0 to 100, with lower values favoring RAM usage and higher values increasing swap usage. Use the appropriate commands to retrieve this value and decide if any changes are needed to optimize your system for specific workloads. Monitoring and tuning swappiness can significantly enhance the performance and stability of your Linux environment.

# cat /proc/sys/vm/swappiness
60

Recommended Training: The Linux Command Line Bootcamp: Beginner To Power User from Colt Steele

Change Swappiness in Linux

To optimize your system’s memory usage and minimize reliance on swap space, you can temporarily set the swappiness value to 0. This configuration prioritizes RAM usage over swap, which can be particularly beneficial for systems with ample memory or applications requiring low latency. Setting swappiness to 0 instructs the Linux kernel to avoid swapping unless absolutely necessary, thereby improving performance for most workloads.

This adjustment affects only the current running session and will revert to the default value after a reboot unless it is permanently configured. Use the appropriate command to apply the change immediately and ensure that your system dynamically optimizes memory handling based on this updated configuration.

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

Verify the current setting of swappiness again.

# cat /proc/sys/vm/swappiness
0

To ensure that the swappiness setting remains consistent across system reboots, you need to make it persistent by adding it to the kernel parameter configuration file. This process involves modifying the /etc/sysctl.conf file or creating a custom configuration file under the /etc/sysctl.d/ directory, which is specifically designed for kernel parameter adjustments.

By defining the swappiness parameter in this file, the system automatically applies your desired configuration at every startup. This approach is particularly useful for maintaining optimal memory management settings on production servers, where minimizing swap usage can improve performance and reduce latency for critical applications. Making this setting persistent ensures stability and consistency in system behavior, even after rebooting.

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

You have successfully change the swappiness for your Linux distribution.

Video Tutorial: How to Adjust Swappiness in Linux

Final Thoughts

Adjusting the swappiness value in Linux is a crucial step to optimize memory management based on your workload requirements. A lower swappiness value ensures that your system prioritizes RAM over swap, which is ideal for performance-critical environments, while a higher value allows more aggressive use of swap space.

By understanding how to check, modify, and make the swappiness setting persistent, you can tailor your Linux system to run more efficiently. Always test your changes in a controlled environment to ensure they meet your performance needs. With these adjustments, you can achieve a balance between memory utilization and system responsiveness, ensuring stable operations for your applications and services.

Whether you need cloud optimization, server management, or automation, I provide comprehensive AWS and Linux services. Hire me on Fiverr to elevate your systems.

Exit mobile version