释放linux的swap内存

来源:互联网 发布:js验证时间格式 编辑:程序博客网 时间:2024/06/09 19:25

这里写图片描述
如上图,一般情况下不会用到swap的,一般物理内存使用在90%以上(默认是这个数,可以自行在/etc/sysctl.conf里设置vm.swappiness参数),但有的时候,内存会被缓存占用,导致系统开始使用swap空间,此时就需要清理下swap了
这里先说下vm.swappiness参数,设置成vm.swappiness=10:就是说当内存使用90%以上才会使用swap空间

第一步:先执行sync命令

#sync

sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息,以防止释放,sync命令则可用来强制将内存缓冲区中的数据立即写入磁盘中。
第二步:(如果仅仅是清理swap的话,这一步可以不执行)

#echo 3 > /proc/sys/vm/drop_caches

此指令输入立即生效,意在释放所有缓存。
关于drop_caches:
drop_caches的详细文档如下:
Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.
To free pagecache:
* echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes:
* echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
* echo 3 > /proc/sys/vm/drop_caches
As this is a non-destructive operation, and dirty objects are notfreeable, the user should run “sync” first in order to make sure allcached objects are freed.
This tunable was added in 2.6.16.
**echo 1:释放页面缓存
echo 2:释放目录文件和inodes
echo 3:释放所有缓存(页面缓存,目录文件和inodes)**
如下图是执行完echo3 的cache的对比:
这里写图片描述

第三步:关闭swap,再开户swap

#swapoff -a#swapon -a

这里写图片描述
现在看swap的used的那一项为零了,说明已经清空

原创粉丝点击