読者です 読者をやめる 読者になる 読者になる

Can't save in background: fork: Cannot allocate memory [Redis]

子processがDBをdiskにdumpしようとした時,理論上は親processと同じだけのmemoryを食う.
けど最近のosの実装だとmemory pageを共有してくれる.
ただ,linuxは子processの処理が終わるまでそいつがどれだけmemoryを食うか判断できないから,親processと同じだけ食うものとして使用するmemoryを計算する.
そのためdatasetの容量よりもmemoryの空き容量が小さいとerrorになる.実際には更新部分の分しか食わないから大丈夫なはず.
overcommit_memoryという設定があってコレを1にしておくとこの問題を解決出来る.(「forkの際のmemory割り当ての仕様が緩和される」らしい.)

FAQ - Redis

Background saving is failing with a fork() error under Linux even if I've a lot of free RAM!
Short answer: echo 1 > /proc/sys/vm/overcommit_memory :)
And now the long one:
Redis background saving schema relies on the copy-on-write semantic of fork in modern operating systems: Redis forks (creates a child process) that is an exact copy of the parent. The child process dumps the DB on disk and finally exits. In theory the child should use as much memory as the parent being a copy, but actually thanks to the copy-on-write semantic implemented by most modern operating systems the parent and child process will share the common memory pages. A page will be duplicated only when it changes in the child or in the parent. Since in theory all the pages may change while the child process is saving, Linux can't tell in advance how much memory the child will take, so if the overcommit_memory setting is set to zero fork will fail unless there is as much free RAM as required to really duplicate all the parent memory pages, with the result that if you have a Redis dataset of 3 GB and just 2 GB of free memory it will fail.
Setting overcommit_memory to 1 says Linux to relax and perform the fork in a more optimistic allocation fashion, and this is indeed what you want for Redis.
A good source to understand how Linux Virtual Memory work and other alternatives for overcommit_memory and overcommit_ratio is this classic from Red Hat Magazine, "Understanding Virtual Memory".