Centos 释放内存(转)

[root@ AY529 ~]# free -m
total used free shared buffers cached
Mem: 2509 1767 741 0 0 1063
-/+ buffers/cache: 703 1805
Swap: 478 0 478
[root@ AY529 ~]#

把内存里的数据暂时写到硬盘里
[root@ AY529 ~]# sync

修改 /proc/sys/vm/drop_caches文件
[root@ AY529 ~]# echo 3 > /proc/sys/vm/drop_caches
[root@ AY529 ~]# cat /proc/sys/vm/drop_caches
3

再看看内存情况
[root@ AY529 ~]# free -m
total used free shared buffers cached
Mem: 2509 668 1840 0 0 89
-/+ buffers/cache: 579 1929
Swap: 478 0 478

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

解决方案

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

这几天发现linux系统内存一直涨,即使把apache和mysql关闭了,内存也不释放,可以使用以下脚本来释放内存:

 
脚本内容:
 
#! /bin/bash
# cache释放:
# To free pagecache:
sync
sync
#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
 利用系统crontab实现每天自动运行:
crontab -e
 输入以下内容:
00 00 * * * /root/Cached.sh
 每天0点释放一次内存,这个时间可以根据自己需要修改设置
在运行./Cached.sh时如果提示错误:Permission denied 权限的问题,可以运行:
 chmod +x .Cached.sh
 此外还可以通过修改/etc/sysctl.conf的方法来达到自动释放缓存的目的。但是内存不够用,最好还是去找找其他的原因,比如程序设置是否合理,是否突发访问量很大,或者程序设计不合理导致内存溢出等,毕竟操作系统本身设计会考虑各方面的问题,强制腾出cache的大小,可能只是把问题给暂时屏蔽了,不利于判断问题的所在。

猜你喜欢

转载自frank1998819.iteye.com/blog/2067717