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:
-
freeCommand: Provides a summary of memory and swap usage in a human-readable format. The-hflag displays values in GB/MB for better readability.free -hOutput includes total swap size, used swap, and free swap (e.g.,
Swap: 2.0G 1.0G 1.0G). -
swapon --showCommand: Lists all currently enabled swap partitions/files with details like device name, type, size, used space, and priority.swapon --showExample output:
NAME TYPE SIZE USED PRIO /dev/sda2 partition 2G 0B -2 /swapfile file 1G 512M 10 -
vmstatCommand: Displays real-time virtual memory statistics, including swap usage (si= swap in,so= swap out). The1flag updates stats every second.vmstat 1Focus on the
siandsocolumns to monitor active swap activity. -
top/htopCommands: Real-time system monitors that include swap usage in their output.htop(install viasudo apt install htop) provides a more intuitive interface with color-coded metrics. -
/proc/meminfoFile: 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) orconky(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:- Create a file (e.g., 1GB):
sudo fallocate -l 1G /swapfile # or `sudo dd if=/dev/zero of=/swapfile bs=1M count=1024` - Set strict permissions (only root can access):
sudo chmod 600 /swapfile - Format the file as swap:
sudo mkswap /swapfile - Enable the swap file:
sudo swapon /swapfile
- Create a file (e.g., 1GB):
-
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
- Temporarily enable a swap file/partition:
-
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/fstabFor 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.confand add:
Apply changes with:vm.swappiness=10sudo sysctl -p
- View current value:
-
Deleting Swap Space:
- For swap files: Disable and remove the file.
Remove thesudo swapoff /swapfile sudo rm /swapfile/etc/fstabentry. - For partitions: Disable the partition, delete its
/etc/fstabentry, and optionally repurpose the disk space.[1,3,4,6,7,9,10,11,12]
- For swap files: Disable and remove the file.