在 CentOS 系统中,可以通过以下几种常用方式检查 Swap(交换分区/交换文件) 的状态。
free 命令(最常用)free -h
示例输出:
total used free shared buff/cache available
Mem: 7.8G 1.2G 2.3G 120M 4.3G 6.1G
Swap: 2.0G 0B 2.0G
✅ 说明:
total:Swap 总大小used:已使用 Swapfree:剩余 Swaptotal = 0,说明 没有启用 Swapswapon 命令(推荐)swapon --show
✅ 正常输出示例:
NAME TYPE SIZE USED PRIO
/dev/sda2 partition 2G 0B -2
❌ 如果没有任何输出,说明 当前系统没有启用 Swap
还可以查看详细信息:
swapon -s
/proc/swapscat /proc/swaps
输出示例:
Filename Type Size Used Priority
/dev/sda2 partition 2097148 0 -2
lsblk
或:
fdisk -l
Swap 分区类型通常是:
82 或 19)vmstatvmstat 1 5
关注 swap 列:
si:swap in(从 swap 读入内存)so:swap out(从内存写入 swap)频繁非零说明 内存压力大。
以上命令 全部适用于:
swapon -a
dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
永久生效(写入 /etc/fstab):
/swapfile swap swap defaults 0 0
✅ 一条命令速查
free -h && swapon --show
如果你愿意,可以把 free -h 或 swapon --show 的输出贴出来,我可以帮你判断 Swap 是否正常、是否需要优化。