温馨提示×

Ubuntu Swapper如何监控和调整

小樊
46
2025-10-30 19:31:43
栏目: 智能运维

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 -h to 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 --show to 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 5 to display swap in/out every second for 5 iterations) or sar (e.g., sar -W 1 to 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.swappiness kernel 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, run sudo sysctl vm.swappiness=10. For permanent changes, add vm.swappiness=10 to /etc/sysctl.conf and apply with sudo sysctl -p.
  • Resize Swap Space: If your system frequently runs low on swap, increase its size. For a swap file (recommended for simplicity):
    1. Create a file: sudo fallocate -l 4G /swapfile (adjust size as needed).
    2. Set permissions: sudo chmod 600 /swapfile (restrict access to root).
    3. Format as swap: sudo mkswap /swapfile.
    4. Enable it: sudo swapon /swapfile.
    5. Make permanent: Add /swapfile none swap sw 0 0 to /etc/fstab.
      To reduce swap size, disable it (sudo swapoff /swapfile), delete the file (sudo rm /swapfile), and remove its /etc/fstab entry. For partition resizing, use tools like gparted to modify partition sizes, then reformat and enable the new swap partition.
  • 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 with sudo swapon --show.
  • Remove Unnecessary Swap: Disable and delete unused swap files/partitions to free up disk space. Run sudo swapon -s to list active swaps, then sudo swapoff -v /swapfile (replace with your swap path) to disable it. Remove the file (sudo rm /swapfile) and delete its /etc/fstab entry.

0