温馨提示×

Debian swapper如何监控与管理

小樊
47
2025-11-10 21:43:29
栏目: 智能运维

Monitoring Debian Swapper (Swap Space)
Monitoring swap space is essential to understand system memory pressure and performance. The following tools and commands help track swap usage in Debian:

  • free Command: Provides a summary of memory and swap usage in a human-readable format. The -h flag displays values in GB/MB for better readability.

    free -h
    

    Output includes total swap size, used swap, and free swap (e.g., Swap: 2.0G 1.0G 1.0G).

  • swapon --show Command: Lists all currently enabled swap partitions/files with details like device name, type, size, used space, and priority.

    swapon --show
    

    Example output:

    NAME      TYPE      SIZE USED PRIO
    /dev/sda2 partition 2G   0B   -2
    /swapfile file      1G   512M 10
    
  • vmstat Command: Displays real-time virtual memory statistics, including swap usage (si = swap in, so = swap out). The 1 flag updates stats every second.

    vmstat 1
    

    Focus on the si and so columns to monitor active swap activity.

  • top/htop Commands: Real-time system monitors that include swap usage in their output. htop (install via sudo apt install htop) provides a more intuitive interface with color-coded metrics.

  • /proc/meminfo File: A detailed pseudo-file with swap-specific entries (e.g., SwapTotal, SwapFree). View it with:

    cat /proc/meminfo | grep Swap
    
  • Graphical Tools: Tools like gnome-system-monitor (GUI) or conky (lightweight desktop widget) offer visualizations of swap usage for easier interpretation.

Managing Debian Swapper (Swap Space)
Managing swap involves creating, enabling/disabling, and configuring swap space to optimize system performance:

  • Creating Swap Space: You can create a swap partition (using fdisk/gparted) or a swap file (more flexible). For swap files:

    1. Create a file (e.g., 1GB):
      sudo fallocate -l 1G /swapfile  # or `sudo dd if=/dev/zero of=/swapfile bs=1M count=1024`
      
    2. Set strict permissions (only root can access):
      sudo chmod 600 /swapfile
      
    3. Format the file as swap:
      sudo mkswap /swapfile
      
    4. Enable the swap file:
      sudo swapon /swapfile
      
  • Enabling/Disabling Swap:

    • Temporarily enable a swap file/partition:
      sudo swapon /swapfile  # or /dev/sdXY for partitions
      
    • Temporarily disable:
      sudo swapoff /swapfile  # or /dev/sdXY
      
    • Disable all swap temporarily:
      sudo swapoff -a
      
  • Persistent Configuration: To retain swap settings after reboot, add an entry to /etc/fstab. For a swap file:

    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
    

    For a partition, use its UUID (find with sudo blkid) or device name:

    UUID=1234-5678-90AB none swap sw 0 0
    
  • Adjusting Swappiness:
    Swappiness (vm.swappiness) controls how aggressively the kernel uses swap (range: 0–100). A lower value prioritizes physical memory; a higher value increases swap usage.

    • View current value:
      cat /proc/sys/vm/swappiness
      
    • Temporarily change (e.g., to 10):
      sudo sysctl vm.swappiness=10
      
    • Permanently change: Edit /etc/sysctl.conf and add:
      vm.swappiness=10
      
      Apply changes with:
      sudo sysctl -p
      
  • Deleting Swap Space:

    • For swap files: Disable and remove the file.
      sudo swapoff /swapfile
      sudo rm /swapfile
      
      Remove the /etc/fstab entry.
    • For partitions: Disable the partition, delete its /etc/fstab entry, and optionally repurpose the disk space.[1,3,4,6,7,9,10,11,12]

0