更多 CentOS Linux 常用命令整理请点击这里。
Swap 释放命令:
swapoff -a;swapon -a
但这里需要注意的是:在执行清空 Swap 的命令时,上述命令会把 Swap 中的数据迁移回物理内存,所以需要保证物理内存的空闲空间是充足的(如果物理内存的空闲空间少于 Swap 已使用的空间,此时若执行清空 Swap 的命令,Ricky 还不清楚会发生什么问题,您可以实验一下)。
所以在执行清空 Swap 的命令前请先清理物理内存(关于释放内存的相关命令,详情请点击这里)。
举例:
当前有一台服务器的内存使用情况如下所示(关于 free -m 命令的解释说明,详情请点击这里):
[root@host ~]# free -m total used free shared buffers cached Mem: 128662 128144 517 117 0 43240 -/+ buffers/cache: 84903 43758 Swap: 15999 15998 1 [root@host ~]#
上述服务器的 Swap 的空闲空间仅剩下 1 MB 了,同时物理内存的空间仅剩 517 MB ,而 cache 使用了物理内存中的 43240 MB ,所以此时我们先清理掉 cache 缓存,释放出大部分的物理内存:
[root@host ~]# sync; echo 1 > /proc/sys/vm/drop_caches [root@host ~]# [root@host ~]# free -m total used free shared buffers cached Mem: 128662 83393 45269 117 0 35 -/+ buffers/cache: 83358 45304 Swap: 15999 15998 1 [root@host ~]#
如上所示,清理掉 cache 缓存后物理内存的空闲空间达到了 45269 MB ,比 Swap 已使用空间更大了,此时执行清理 Swap 的命令(即关掉 Swap 后再开启,让 Swap 中的数据迁移到物理内存):
[root@host ~]# swapoff -a;swapon -a
此时如果您再开启一个命令行界面(比如另一个 ssh 连接)您将会看到 Swap 中的数据正一点一点地迁移到物理内存:
[root@host ~]# free -m total used free shared buffers cached Mem: 128662 84072 44590 140 3 570 -/+ buffers/cache: 83498 45164 Swap: 15905 15905 0 [root@host ~]# [root@host ~]# free -m total used free shared buffers cached Mem: 128662 85098 43564 147 3 1531 -/+ buffers/cache: 83563 45099 Swap: 15861 15861 0 [root@host ~]# [root@host ~]# free -m total used free shared buffers cached Mem: 128662 86347 42315 181 3 2589 -/+ buffers/cache: 83754 44908 Swap: 15669 15669 0 [root@host ~]#
这台服务器清理 Swap 大约耗时 4 个小时左右,最终效果如下所示:
[root@host ~]# free -m
total used free shared buffers cached
Mem: 128662 105423 23238 2351 3 7513
-/+ buffers/cache: 97906 30755
Swap: 15999 0 15999
[root@host ~]#
至此,清理完成。
参考自:
- https://blog.csdn.net/hknaruto/article/details/47044575
发表评论?