如果細心的同學,可能會發現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,驗證警告是否消失