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:
sudo fallocate -l 1G /swapfile # or `sudo dd if=/dev/zero of=/swapfile bs=1M count=1024`
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Enabling/Disabling Swap:
sudo swapon /swapfile # or /dev/sdXY for partitions
sudo swapoff /swapfile # or /dev/sdXY
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.
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
/etc/sysctl.conf and add:vm.swappiness=10
Apply changes with:sudo sysctl -p
Deleting Swap Space:
sudo swapoff /swapfile
sudo rm /swapfile
Remove the /etc/fstab entry./etc/fstab entry, and optionally repurpose the disk space.[1,3,4,6,7,9,10,11,12]