如果细心的同学,可能会发现Redis启动时,应该会给出如下警告信息:

1 [29446] 07 Sep 08:14:14.162 # 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 take effect.

顺便提一下:overcommit_memory的作用。

overcommit_memory:
 
This value contains a flag that enables memory overcommitment.
 
When this flag is 0, the kernel attempts to estimate the amount
of free memory left when userspace requests more memory.
 
When this flag is 1, the kernel pretends there is always enough
memory until it actually runs out.
 
When this flag is 2, the kernel uses a "never overcommit"
policy that attempts to prevent any overcommit of memory.
 
This feature can be very useful because there are a lot of
programs that malloc() huge amounts of memory "just-in-case"
and don't use much of it.

The default value is 0. (很容易联想到上面的:WARNING overcommit_memory is set to 0!)
 
See Documentation/vm/overcommit-accounting and
security/commoncap.c::cap_vm_enough_memory() for more information.
==============================================================
overcommit_ratio:
When overcommit_memory is set to 2, the committed address
space is not permitted to exceed swap plus this percentage
of physical RAM.  See above.

明白了原因,那就根据提示进行修改吧!

方法1:只影响本次
1.验证系统中overcommit_memory的值,执行如下命令
sysctl -n vm.overcommit_memory

2.设置overcommit_memory的值为1
sysctl -w vm.overcommit_memory=1

3.验证系统中overcommit_memory的值是否被改成了1
sysctl -n vm.overcommit_memory

方法2:(需重启)
1.将vm.overcommit_memory = 1添加到/etc/sysctl.conf中
2.重启电脑


重新运行redis,验证警告是否消失