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=(e.g.,sudo sysctl vm.swappiness=10). This updates the running kernel configuration immediately. - Direct File Write:
Useecho(e.g.,| sudo tee /proc/sys/vm/swappiness 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=(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=. 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.
以上就是关于“Ubuntu Swap怎么调整内核参数”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm