Redis 填坑系列之——内存分配

redis启动报错,查看日志如下,出现三个警告错误:

17328:M 22 Jun 2020 15:42:39.000 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
17328:M 22 Jun 2020 15:42:39.000 # Server initialized
17328:M 22 Jun 2020 15:42:39.000 # 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 'sysct
l vm.overcommit_memory=1' for this to take effect.
17328:M 22 Jun 2020 15:42:39.000 # 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.
17328:M 22 Jun 2020 15:42:39.000 # Fatal error loading the DB: Permission denied. Exiting.

问题一:# WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

翻译大概就是:/proc/sys/net/core/somaxconn 设置128这个值,因此无法强制执行TCP待办事项。然后建议设置为 511 .

解决办法:可以临时设置,或者设置永久值:

# 临时解决:
echo 511>/proc/sys/net/core/somaxconn

# 永久解决:
vi etc/sysctl.conf
## 设置值:
net.core.somaxconn= 1024
## 使生效
sysctl -p

问题二:# 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 'sysct
l vm.overcommit_memory=1' for this to take effect.

翻译大概就是:overcommit_memory 值设置为0,在内存不足的情况下,后台保存可能会失败。

扩展:

内核参数overcommit_memory 

overcommit_memory是内存分配策略

设置内存分配策略(可选,根据服务器的实际情况进行设置)
/proc/sys/vm/overcommit_memory

可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存

什么是Overcommit和OOM

      Linux对大部分申请内存的请求都回复"yes",以便能跑更多更大的程序。因为申请内存后,并不会马上使用内存。这种技术叫做Overcommit。当linux发现内存不足时,会发生OOM killer(OOM=out-of-memory)。它会选择杀死一些进程(用户态进程,不是内核线程),以便释放内存。

       当oom-killer发生时,linux会选择杀死哪些进程?选择进程的函数是oom_badness函数(在mm/oom_kill.c中),该函数会计算每个进程的点数(0~1000)。点数越高,这个进程越有可能被杀死。每个进程的点数跟oom_score_adj有关,而且oom_score_adj可以被设置(-1000最低,1000最高)。

解决办法:修改内核参数

# 方式一:
vim /etc/sysctl.conf
修改为:
vm.overcommit_memory=1

sysctl -p ## 使配置文件生效

# 方式二:
sysctl vm.overcommit_memory=1

# 方式三:
echo 1 > /proc/sys/vm/overcommit_memory    # 不需要启机器就生效

问题三:# 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.

翻译大概意思:redis建议我们关闭THP,以免造成相关问题。

扩展:Transparent Huge Pages (THP) THP 透明大页,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章