Mongodb在内存不足时终止

我有以下configuration:

  • 运行三个docker容器的主机:
    • MongoDB的
    • Redis的
    • 一个使用前两个容器来存储数据的程序

Redis和Mongodb都被用来存储大量的数据。 我知道Redis需要保留所有的数据在RAM中,我很好。 不幸的是,Mongo开始占用大量内存,一旦主机内存已满(我们在这里讨论32GB),Mongo或Redis都会崩溃。

我已阅读了以下关于这个问题的以前的问题:

  1. 限制MongoDB RAM使用情况 :显然,大多数RAM都被WiredTigercaching使用
  2. MongoDB限制内存 :这里显然问题是日志数据
  3. 限制MongoDB中的RAM内存使用量 :在这里,他们build议限制mongo的内存,以便为其caching/日志/数据使用较less的内存
  4. MongoDB使用太多的内存 :这里他们说这是WiredTigercaching系统,它倾向于使用尽可能多的RAM来提供更快的访问。 他们还表示it's completely okay to limit the WiredTiger cache size, since it handles I/O operations pretty efficiently
  5. 有没有限制mongodb内存使用的选项? :再次caching,他们还添加了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
  6. MongoDB的索引/内存关系 :quote: 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. 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.
  7. 如何释放Mongodb使用的caching? :与5中的答案相同。

现在我从所有这些答案中看出来的是:

  1. 为了更快的访问,Mongo将所有的索引放在RAM中会更好。 不过,在我的情况下,由于我有一个非常快速的SSD,所以我对部分驻留在磁盘上的索引很好。
  2. RAM主要用于Mongo的caching。

考虑到这一点,我期待Mongo尝试尽可能多地使用RAM空间,但也能够以很less的RAM空间工作,并从磁盘获取大部分内容。 不过,我使用--memory--memory-swap限制了Mongo Docker容器的内存(例如8GB),但是Mongo不是从磁盘中获取内容,而是一旦内存耗尽,就会崩溃。

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

感谢@ AlexBlex的评论我解决了我的问题。 显然,问题是Docker将容器的内存限制在8GB但有线Tiger仍然试图占用50% - 1GB的内存(在我的情况下是15 GB )。 通过使用这个configuration选项 ,将wiredTiger的caching大小设置为小于Docker分配的值可以解决问题。

Interesting Posts