Mongodb在内存不足时终止

时间:2017-12-04 17:46:14

标签: mongodb docker limit ram

我有以下配置:

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

Redis和Mongodb都用于存储大量数据。我知道Redis需要将所有数据保存在RAM中,我对此很好。不幸的是,发生的事情是Mongo开始占用大量RAM并且一旦主机RAM满了(我们在这里谈论32GB),Mongo或Redis都会崩溃。

我之前已经阅读了以下有关此问题:

  1. Limit MongoDB RAM Usage:显然大多数RAM已被WiredTiger缓存用完
  2. MongoDB limit memory:显然问题是日志数据
  3. Limit the RAM memory usage in MongoDB:他们建议限制mongo的内存,以便为其缓存/日志/数据使用较少的内存
  4. 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
  5. 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
  6. MongoDB index/RAM relationship: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.
  7. how to release the caching which is used by Mongodb?:与5中的答案相同。
  8. 现在我似乎从所有这些答案中理解的是:

    1. 为了更快地访问,Mongo将所有索引都安装在RAM中会更好。但是,在我的情况下,我可以使用部分驻留在磁盘上的索引,因为我有一个非常快的SSD。
    2. RAM主要用于Mongo的缓存。
    3. 考虑到这一点,我希望Mongo尝试尽可能多地使用RAM空间,但是能够在没有RAM空间的情况下运行并从磁盘中获取大部分内容。但是,我通过使用--memory--memory-swap来限制Mongo Docker容器的内存(例如8GB),但不是从磁盘中取出东西,Mongo只是在它耗尽时就崩溃了记忆。

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

1 个答案:

答案 0 :(得分:4)

感谢@ AlexBlex的评论,我解决了我的问题。显然问题在于Docker将容器的RAM限制为8GB,但是有线网络存储引擎仍在尝试用尽其总系统RAM的50% - 1GB缓存(其中)在我的情况下会是15 GB)。

使用this configuration option使用低于Docker分配的值解决问题,限制了wireTiger的缓存大小。