Mongodb 内存不足时终止

Mongodb terminates when it runs out of memory

我有以下配置:

Redis和Mongodb都是用来存储海量数据的。我知道 Redis 需要将其所有数据保存在 RAM 中,我对此没有意见。不幸的是,Mongo 开始占用大量 RAM,一旦主机 RAM 已满(我们在这里谈论的是 32GB),Mongo 或 Redis 就会崩溃。

我已阅读以下有关此问题的先前问题:

  1. :显然大多数 RAM 都被 WiredTiger 缓存用完了
  2. MongoDB limit memory:显然这里的问题是日志数据
  3. Limit the RAM memory usage in MongoDB:他们在这里建议限制 mongo 的内存,以便它为 cache/logs/data
  4. 使用更小的内存
  5. MongoDB using too much memory:这里他们说是 WiredTiger 缓存系统,它倾向于使用尽可能多的 RAM 来提供更快的访问。他们还声明 it's completely okay to limit the WiredTiger cache size, since it handles I/O operations pretty efficiently
  6. Is there any option to limit mongodb memory usage?:再次缓存,他们还添加了MongoDB uses the LRU (Least Recently Used) cache algorithm to determine which "pages" to release, you will find some more information in these two questions
  7. MongoDB index/RAM relationship: 引用: MongoDB keeps what it can of the indexes in RAM. They'll be swaped out on an LRU basis. You'll often see documentation that suggests you should keep your "working set" in memory: if the portions of index you're actually accessing fit in memory, you'll be fine.
  8. how to release the caching which is used by Mongodb?: 与 5.
  9. 相同的答案

现在我从所有这些答案中了解到的是:

  1. 为了加快访问速度,Mongo 将所有索引放入 RAM 会更好。但是,就我而言,我可以接受部分驻留在磁盘上的索引,因为我有一个非常快的 SSD。
  2. RAM 主要用于 Mongo 的缓存。

考虑到这一点,我期待 Mongo 尝试使用尽可能多的 RAM space 但也能够在很少的 RAM space 下运行并从磁盘中获取大部分内容.但是,我通过使用 --memory--memory-swap 限制了 Mongo Docker 容器的内存(例如 8GB),而不是从磁盘中获取内容,Mongo刚 运行 内存不足就崩溃了。

如何强制 Mongo 仅使用可用内存并从磁盘中获取所有不适合内存的内容?

感谢@AlexBlex 的评论,我解决了我的问题。显然问题是 Docker 将容器的 RAM 限制为 8GB 但 wiredTiger 存储引擎仍在尝试用尽整个系统 RAM 的 50% - 1GB 作为它的缓存(在我的情况下会已经 15 GB).

通过使用 this configuration option 将 wiredTiger 的缓存大小设置为小于 Docker 分配的值解决了问题。