目录

一、vm.overcommit_memory

二、swappiness

三、THP

四、OOM killer

五、ulimit

六、TCP backlog


一、vm.overcommit_memory

# WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to
vm.overcommit_memory=0,代表如果没有可用内存,就申请内存失败,对应到Redis就是执行fork失败,在Redis的日志会出现:Cannot allocate memory
所以上边的日志会提示overcommit_memory 配置 0 改为1,这样可以让fork操作在低内存的情况下也可以执行成功.overcommit_memory 用来配置内存分配策略

1.overcommit参数值



含义

0

表示内核是否有足够的可用内存,如果有足够的可用内存,内存申请通过,否则内存申请失败,并把错误返回给应用进程

1

表示内核允许超量使用内存直到用完为止

2

表示内核绝不过量的("never overcommit")使用内存,即系统整个内存地址空间不能超过swap+50%的RAM值,50%是overcommit_ratio的默认值,此参数同样支持修改


 2.配置方式

echo "vm.overcommit_memory=1" >> /etc/sysctl.conf 
 sysctl vm.overcommit_memory=1

二、swappiness

不是要等到所有物理内存用完才使用swap,参数swppiness会决定操作系统使用swap的倾向程度。swappiness取值范围是0~100,swappiness的值越大,说明操作系统可能使用swap的概率越高,swappiness值越低,表示操作系统更加倾向于使用物理内存。swap的默 认值是60

 1.swapness参数配置 


策略

0

Linux3.5及以上:用OOM killer也不使用swap

Linux3.4及更早: 用swap也不使用OOM killer

1

Linux3.5及以上:用swap也不使用OOM killer

60

默认值

100

操作系统会主动的使用swap

2.配置方式

临时生效: echo {bestvalue} > /proc/sys/vm/swappiness
 持久生效需再执行:echo vm.swappiness={bestvalue} >> /etc/sysctl.conf

 3.检测swap

free -m : 以M为单位查看swap使用情况
 vmstat 1 : 每秒输出一次查看swap使用情况(看si so参数是否使用)
 redis-cli -a [pwd] -h [ip] -p [port] info server|grep process_id 查看redis的pid,我这里是容器部署的,所以pid为1
 cat /proc/{pid}/smaps |grep Swap 查看此pid下的各个内存块中的swap信息求和

 

Linux 怎么修改redis配置文件内容_物理内存

Linux 怎么修改redis配置文件内容_redis_02

 4.注

linux >3.5 vm.swapniess=1实现如下两个目标:
物理内存充足,使redis足够快
物理内存不足,避免redis死掉

三、THP

# WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

Linux kernel在2.6.38内核增加了THP特性,支持大内存页(2MB)分配,默认开启。当开启时可以降低fork子进程的速度,但fork操作之后,每个内存页从原来4KB变为2MB,会大幅增加重写期间父进程内存消耗。同时每次写命令引起的复制内存页单位放大了512倍,会拖慢写操作的执行时间,导致大量写操作慢查询因此Redis日 志中建议将此特性进行禁用,禁用方法如下:

临时生效 echo never > /sys/kernel/mm/transparent_hugepage/enabled
 永久生效 vim /etc/rc.local
 --------------------------------------------------------------
 if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
 fi
 if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
    echo never > /sys/kernel/mm/transparent_hugepage/defrag
 fi


--------------------------------------------------------------

四、OOM killer

OOM killer会在可用内存不足时选择性地杀掉用户进程,OOM killer进程会为每个用户进程设置一个权值,这个权值越高,被“下手”的概率就越高,反之概率越低。 
每个进程的权值存放在/proc/{progress_id}/oom_score中,这个受/proc/{progress_id}/oom_adj的控制,oom_adj在不同的Linux版本中最小值不同,可以参考Linux源码中oom.h(从-15到-17)。当oom_adj设置为最小值 时,该进程将不会被OOM killer杀掉,设置方法如下:
 echo {value} > /proc/${process_id}/oom_adj对于Redis所在的服务器来说,可以将所有Redis的oom_adj设置为最低值或者稍小的值,降低被OOM killer杀掉的概率(不适用于容器部署): 
 for redis_pid in $(pgrep -f "redis-server") do echo -17 > /proc/${redis_pid}/oom_adj done

五、ulimit

在Linux中,可以通过ulimit查看和设置系统当前用户进程的资源数。其中ulimit-a命令包含的openfiles参数,是单个用户同时打开的最大文件个数:
Redis允许同时有多个客户端通过网络进行连接可以通过配置 maxclients来限制最大客户端连接数。对Linux操作系统来说,这些网络连接都是文件句柄。假设当前open files是4096,那么启动Redis时会看到如下日志:
====================================================================
 # You requested maxclients of 10000 requiring at least 10032 max file descriptors. 
 # Redis can’t set maximum open files to 10032 because of OS error: Operation not permitted. 
 # Current maximum open files is 4096. Maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase ‘ulimit –n’.
 =====================================================================1.服务端maxclients=10000,至少需要10032个fd.多出32个是redis内部服务最多占用fd个数
 2.redis不能将fd配置为10032,因为他没有权限
 3.查询ulimit当前设置最大fd为4096,建议maxclients设置为4064,若需要更高的maxclients请使用ulimit -n来设置: ulimit –Sn {max-open-files}

六、TCP backlog

Redis默认的tcp-backlog值为511,可以通过修改配置tcp-backlog进行调整,如果Linux的tcp-backlog小于Redis设置的tcp-backlog,那么在Redis启动时会看到如下日志:
 =====================================================================
 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/ net/core/somaxconn is set to the lower value of 128. 
 =====================================================================
 查看方法: cat /proc/sys/net/core/somaxconn 128
 修改方法: echo 511 > /proc/sys/net/core/somaxconn