centos服务器free内存优化

背景:

服务器内存free经常200M左右,cache过多内存不释放,服务器不稳定,风险较大。

原因:

系统当前默认的下列内核参数满足不了当前需求,设置偏低(偏大了更危险)

vm.min_free_kbytes  目前min普遍在42M在84M之间或者多一点点

vm.vfs_cache_pressure  目前普遍是100

 

说明:

设定这个参数时请小心,因为该值过低和过高都有问题。
min_free_kbytes 太低可防止系统重新利用内存。这可导致系统挂起并让 OOM 杀死多个进程。
但将这个参数值设定太高(占系统总内存的 5-10%)会让您的系统很快会内存不足。Linux 的设计是使用所有可用 RAM 缓存文件系统数据。设定高 min_free_kbytes 值的结果是在该系统中花费太多时间重新利用内存。

 上面2个参数解释参考链接  (有人可能说下面有的网址打不开,你懂的)

https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/6/html/performance_tuning_guide/s-memory-tunables

https://www.cnblogs.com/panfeng412/p/drop-caches-under-linux-system-2.html
https://www.cnblogs.com/muahao/p/8082997.html
https://www.dazhuanlan.com/2019/10/09/5d9d72d0d3a0e/

 另外  vm.extra_free_kbytes   这个参数看网上是在redhat/centos 5的系统有,redhat/centos 7系统没有了

 

/proc/sys/vm/drop_caches     参数参考链接  http://linuxperf.com/?p=201

解决:

1.修改内核参数

sysctl -w vm.min_free_kbytes=614400  # 设置 min 内存为600Mb

sysctl -w vm.vfs_cache_pressure=200  # 设置回收倾向,数值越大倾向性越强,参考上面链接

echo 3 > /proc/sys/vm/drop_caches  这个参数 我觉得可以和上面2个一块改,增大内存回收范围

 

2.服务器操作:

新建ansible inventory文件autofree和分组autofree

ansible -i autofree autofree  -m shell  -b -a "sysctl -w vm.min_free_kbytes=614400;sysctl -w vm.vfs_cache_pressure=200"

ansible -i autofree autofree -m copy -a "src=/home/suser/tongji-mem.sh dest=/tmp/tongji-mem.sh"

 

******

cat tongji-mem.sh
T=min;sum=0;for i in `cat /proc/zoneinfo |grep $T | awk '{print $NF}'`;do sum=`echo "$sum+$i" |bc`;done;sum=`echo "$sum*4/1024" |bc`;echo "sum=${sum} MB"
T=low;sum=0;for i in `cat /proc/zoneinfo |grep $T | awk '{print $NF}'`;do sum=`echo "$sum+$i" |bc`;done;sum=`echo "$sum*4/1024" |bc`;echo "sum=${sum} MB"
T=high;sum=0;for i in `cat /proc/zoneinfo |grep $T | awk '{print $NF}'`;do sum=`echo "$sum+$i" |bc`;done;sum=`echo "$sum*4/1024" |bc`;echo "sum=${sum} MB"

******

验证

ansible -i autofree autofree -m shell -b -a "bash /tmp/tongji-mem.sh"

原文地址:https://www.cnblogs.com/argv/p/13178269.html