On a very old Linux machine, with an extremely weak CPU, little RAM and an SSD drive what is the appropriate swappiness value? Keep in mind that despite the low RAM, the actual performance bottleneck on this machine is almost always due to the CPU
Assistant
The swappiness parameter in Linux controls how aggressively the kernel will swap memory pages out of RAM to the swap space (in this case, your SSD) versus keeping them in RAM. The value can range from 0 to 100:
A value of 0 means that the kernel will avoid swapping out pages unless absolutely necessary.
User
Waht do swappiness values over 100 mean (from kernel 5.8 on it seems it can be adjusted up to 200)
Assistant
In Linux kernel versions starting from 5.8, the swappiness parameter can be set to values greater than 100, with the upper limit set to 200. This expanded range effectively provides greater control over the swapping behavior of the system.
User
Can I change swappiness on the fly for the current session?
Assistant
Yes, you can change the swappiness value on the fly for the current session without needing to reboot the system. To do this, you can use the sysctl command or directly write to the /proc/sys/vm/swappiness file.
Changing Swappiness on the Fly
Check Current Swappiness:
You can check the current swappiness value by running:
cat /proc/sys/vm/swappiness
Set New Swappiness Value:
To change the swappiness value, you can use either of the following methods:
User
Is that valid for sysvinit machines too?
Assistant
Yes, the method of changing the swappiness value on the fly using either sysctl or writing directly to /proc/sys/vm/swappiness works on all Linux systems regardless of the init system used, including those using sysvinit.
Key Points
Kernel Configuration: The ability to change kernel parameters like swappiness is part of the Linux kernel, and not dependent on the init system (sysvinit, , etc.). As long as you have a Linux kernel that supports the setting (which has been present for many years), you can change it dynamically.
A value of 100 means that the kernel will swap out pages aggressively to keep as much free memory available as possible.
Given your situation—a very old Linux machine with a weak CPU and limited RAM where the performance bottleneck is mainly due to the CPU—it's typical to adjust the swappiness value to favor RAM usage over swapping.
Recommendations
Set swappiness to a Low Value: A swappiness value of 10 is often a good starting point for systems with limited RAM and a weak CPU. This means the system will only swap out pages when it has less than 10% of RAM free, prioritizing keeping active processes in memory.
Monitor Performance: After adjusting swappiness, monitor the system's performance under typical loads. If you notice performance issues or frequent swapping, you can further tweak the value. You can try values between 0 and 10 depending on how much RAM you have available.
Make It Permanent: If you find a suitable swappiness value, you can make it permanent by adding or modifying the line in /etc/sysctl.conf:
vm.swappiness = 10
After that, apply the changes by running:
sudo sysctl -p
Additional Considerations
Use of SSD: Since you have an SSD, swapping might be faster than on traditional HDDs. However, frequent swapping can still degrade performance, especially if the CPU is your bottleneck.
RAM Capacities: If the system has very low RAM (e.g., less than 512 MB), you may need to be even more conservative with swappiness.
Other Optimization: In addition to adjusting the swappiness, consider optimizing running applications for low-resource usage and disabling any unnecessary services to reduce CPU load where possible.
In summary, starting with a swappiness value of 10 is advisable on this machine to keep pages in RAM for CPU-bound tasks while avoiding excessive swapping.
Interpretation of Swappiness Values Over 100
Values Greater than 100: Setting swappiness to values above 100 changes the behavior to favor swapping more aggressively than the default behavior. A swappiness of 200, for example, means that the kernel will prioritize swapping out pages more aggressively compared to lower values or the default.
Effect of Higher Values: While a swappiness of 100 generally indicates a balanced approach towards using RAM and swap space, values above 100 suggest a tendency to:
Swap out more pages even when there’s still available RAM.
Keep more memory available for potential new allocations at the cost of cached data or actively used pages.
May lead to increased I/O and possibly reduced overall system performance, especially on systems where CPU performance is a bottleneck.
Considerations for Using High Swappiness Values
Use Cases: Setting a high swappiness, such as 150 or 200, might be appropriate in specific scenarios where:
You have an application that benefits from keeping a larger pool of free RAM available for new allocations.
You are running many memory-intensive applications simultaneously, and you need to maintain responsiveness by freeing memory quickly.
Performance Trade-off: However, you should be cautious, as increasing swappiness excessively could lead to situations where memory pages that are still in use are swapped out, resulting in performance degradation due to increased latency when accessing these pages later.
Experimentation: As with any tuning parameter, it’s best to experiment with different values and monitor system performance. You can start with a moderate value over 100, like 120 or 150, and then refine it based on your specific workloads and performance.
Summary
In summary, swappiness values over 100 increase the likelihood of swapping out pages even when there's free RAM available, which can be useful in certain memory-intensive scenarios. However, adjustments should be made carefully, keeping in mind the overall impact on system performance and the specific characteristics of your workloads.
Using sysctl:
sudo sysctl -w vm.swappiness=<new_value>
Replace <new_value> with the desired swappiness value (e.g., 10, 50, 200, etc.).
Directly Writing to the File:
Alternatively, you can echo the new value directly to the /proc filesystem:
echo <new_value> | sudo tee /proc/sys/vm/swappiness
Verify the Change:
After setting the new value, you can verify that it has been updated by running:
cat /proc/sys/vm/swappiness
This will show you the currently active swappiness setting.
Note
Changing the swappiness value using these methods affects only the current session. If you reboot the system, the swappiness value will revert to the value defined in the /etc/sysctl.conf file (or its related configuration). To make the change permanent, you would need to edit that file and add or modify the following line:
vm.swappiness = <new_value>
Example
Here’s a quick example of setting swappiness to 10 for the session:
sudo sysctl -w vm.swappiness=10
Using the methods described, you can experiment with different swappiness values and see how they affect performance in real time.
systemd
swappiness
Commands Remain the Same:
Using sysctl:
sudo sysctl -w vm.swappiness=<new_value>
Writing directly to /proc:
echo <new_value> | sudo tee /proc/sys/vm/swappiness
Configuration Persistence
While the methods to change swappiness on the fly are valid, the method of making it permanent does involve checking the configuration files on your system. Here's how to make it persistent:
Editing sysctl Configuration:
On most systems, you can create or edit /etc/sysctl.conf and add the following line:
vm.swappiness = <new_value>
After editing, apply the changes with:
sudo sysctl -p
Legacy sysvinit Configurations:
For systems using sysvinit, you might also find an /etc/rc.local file where system startup commands can be placed. You can add your swappiness command here to ensure it's set every time the system boots:
The methods for changing the swappiness value dynamically are the same across different init systems, including sysvinit. You can adjust it as needed, and remember that permanent changes can be applied through configuration files or startup scripts.