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 透明大頁,

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