Ubuntu Swapper如何监控和调整
导读:Monitoring Ubuntu Swapper (Swap Usage  To effectively manage the Swapper (swap space  in Ubuntu, start by monitoring its...
Monitoring Ubuntu Swapper (Swap Usage)
To effectively manage the Swapper (swap space) in Ubuntu, start by monitoring its usage to understand system behavior:
- Check Total and Used Swap: Use free -hto view a human-readable summary of physical memory (RAM) and swap space usage. This command shows total swap size, used swap, and free swap alongside RAM metrics.
- View Detailed Swap Entries: Run sudo swapon --showto list all active swap partitions/files, including their sizes and priorities. This helps identify which swap devices are currently in use.
- Advanced System Monitoring: For real-time insights into swap activity, use tools like vmstat(e.g.,vmstat 1 5to display swap in/out every second for 5 iterations) orsar(e.g.,sar -W 1to monitor page swap activity). These commands help correlate swap usage with overall system performance.
- Deep Dive with SystemTap: For detailed process-level monitoring, install SystemTap and run scripts (e.g., sudo stap -e 'probe scheduler.cpu_off { printf("%20s (%5d) -> %20s (%5d), idle:%d\n", task_execname(task_prev), task_pid(task_prev), task_execname(task_next), task_pid(task_next), idle); } ') to track when the CPU switches to idle states, indicating potential swapper activity.
Adjusting Ubuntu Swapper Settings
Once you’ve monitored swap usage, optimize its configuration to balance performance and resource usage:
- Tune Swappiness Parameter: The vm.swappinesskernel parameter controls how aggressively the system uses swap (range: 0–100). A lower value (e.g., 10) reduces swap reliance, preferring RAM; a higher value (e.g., 60, the Ubuntu default) increases swap usage. To temporarily change it, runsudo sysctl vm.swappiness=10. For permanent changes, addvm.swappiness=10to/etc/sysctl.confand apply withsudo sysctl -p.
- Resize Swap Space: If your system frequently runs low on swap, increase its size. For a swap file (recommended for simplicity):
- Create a file: sudo fallocate -l 4G /swapfile(adjust size as needed).
- Set permissions: sudo chmod 600 /swapfile(restrict access to root).
- Format as swap: sudo mkswap /swapfile.
- Enable it: sudo swapon /swapfile.
- Make permanent: Add /swapfile none swap sw 0 0to/etc/fstab.
 To reduce swap size, disable it (sudo swapoff /swapfile), delete the file (sudo rm /swapfile), and remove its/etc/fstabentry. For partition resizing, use tools likegpartedto modify partition sizes, then reformat and enable the new swap partition.
 
- Create a file: 
- Adjust Swap Priority: For systems with multiple swap partitions/files, prioritize their usage with sudo swapon -p < priority> /dev/swap_partition(higher numbers = higher priority). Lower-priority swaps are used only when higher-priority ones are full. List priorities withsudo swapon --show.
- Remove Unnecessary Swap: Disable and delete unused swap files/partitions to free up disk space. Run sudo swapon -sto list active swaps, thensudo swapoff -v /swapfile(replace with your swap path) to disable it. Remove the file (sudo rm /swapfile) and delete its/etc/fstabentry.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu Swapper如何监控和调整
本文地址: https://pptw.com/jishu/740113.html
