Ubuntu Swap怎么调整内核参数
导读:Adjusting Kernel Parameters for Ubuntu Swap 1. Key Kernel Parameter: vm.swappiness vm.swappiness controls the kernel’s t...
Adjusting Kernel Parameters for Ubuntu Swap
1. Key Kernel Parameter: vm.swappiness
vm.swappiness controls the kernel’s tendency to move inactive memory pages from RAM to swap space. It ranges from 0 to 100:
- Higher values (e.g., 60, default): The kernel aggressively swaps out pages, which can increase disk I/O but free up RAM for caching.
- Lower values (e.g., 10–30): The kernel avoids swapping unless RAM is critically low, prioritizing application performance.
- Value = 0: The kernel will only swap to prevent out-of-memory (OOM) errors—not a complete disablement.
2. Temporary Modification (Restart-Effective)
To test the impact of vm.swappiness without rebooting:
- Using
sysctl(Recommended):
Runsudo sysctl vm.swappiness=< value>(e.g.,sudo sysctl vm.swappiness=10). This updates the running kernel configuration immediately. - Direct File Write:
Useecho < value> | sudo tee /proc/sys/vm/swappiness(e.g.,echo 10 | sudo tee /proc/sys/vm/swappiness). This directly modifies the kernel’s runtime parameter interface.
3. Permanent Modification (Boot-Effective)
For changes to persist after a reboot, edit the system’s kernel parameter configuration:
- Method 1: Edit
/etc/sysctl.conf
Open the file with a text editor (e.g.,sudo nano /etc/sysctl.conf). Add or uncomment the linevm.swappiness=< value>(e.g.,vm.swappiness=10). Save the file and runsudo sysctl -pto apply changes without rebooting. - Method 2: Use
/etc/sysctl.d/(Modern Approach)
Create a custom configuration file (e.g.,sudo nano /etc/sysctl.d/99-custom-swap.conf) and addvm.swappiness=< value>. Runsudo sysctl --systemto load all configurations (including the new file), which overrides/etc/sysctl.confif there are conflicts.
4. Verification
After modification, confirm the new value is applied:
cat /proc/sys/vm/swappiness # Should display the updated value (e.g., 10)
Additional Recommendations
- Desktop/Development Environments: Set
vm.swappinessto 10–30 to balance performance and memory availability. - High-Performance Servers (Databases): Use 1–10 to minimize swap usage and avoid I/O bottlenecks for critical applications.
- SSD Optimization: If using an SSD, combine a lower
vm.swappiness(10–30) with a swap file on the SSD to reduce latency. For HDDs, keepvm.swappinessat 10–30 to mitigate slow swap I/O.
By adjusting vm.swappiness, you can optimize Ubuntu’s swap behavior to match your system’s workload and hardware, ensuring better performance under memory pressure.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu Swap怎么调整内核参数
本文地址: https://pptw.com/jishu/744619.html
