Mongo: Mongo will run out of memory when memory limits are set

Created on 8 Nov 2018  路  9Comments  路  Source: docker-library/mongo

I am running mongo container in Ubuntu development server and we had recently some performance issues and memory leaks.
When you use official mongo image with default configuration, mongo will allocate size of the internal WiredTiger cache according to memory size of the host machine, but not the actual container itself. If you use any memory limitation for the container, mongo will ignore it. More info in manual:

If you run mongod in a container (e.g. lxc, cgroups, Docker, etc.) that does not have access to all of the RAM available in a system, you must set --wiredTigerCacheSizeGB to a value less than the amount of RAM available in the container. The exact amount depends on the other processes running in the container.

I am proposing to check actual memory limit in docker-entrypoint.sh and set --wiredTigerCacheSizeGB or storage.wiredTiger.engineConfig.cacheSizeGB in config to repond to this limit. Memory limit could be seen e.g. in _/sys/fs/cgroup/memory/memory.limit_in_bytes_

Request

Most helpful comment

Interesting -- given that it's specific to WiredTiger (and _only_ controls WiredTiger), I'm a little wary of doing this automatically, but it might be worth pointing out in our documentation.

As for using it, it should be as easy as adding --wiredTigerCacheSizeGB to your container command. In a Stack/Compose YAML, that'd be something like command: --wiredTigerCacheSizeGB 1 (for limiting to 1GB).

The following additional bit from https://docs.mongodb.com/manual/reference/program/mongod/#cmdoption-mongod-wiredtigercachesizegb is interesting (and further lends credence to my thought that we should leave specifying this up to the user):

To accommodate the additional consumers of RAM, you may have to decrease WiredTiger internal cache size.

All 9 comments

I am having similar issues. I want MongoDB to use a smaller percentage of the host OS's memory. I have come across the config named wiredTigerCacheSizeGB and I am looking for a way to set it in the docker container when it runs.

Interesting -- given that it's specific to WiredTiger (and _only_ controls WiredTiger), I'm a little wary of doing this automatically, but it might be worth pointing out in our documentation.

As for using it, it should be as easy as adding --wiredTigerCacheSizeGB to your container command. In a Stack/Compose YAML, that'd be something like command: --wiredTigerCacheSizeGB 1 (for limiting to 1GB).

The following additional bit from https://docs.mongodb.com/manual/reference/program/mongod/#cmdoption-mongod-wiredtigercachesizegb is interesting (and further lends credence to my thought that we should leave specifying this up to the user):

To accommodate the additional consumers of RAM, you may have to decrease WiredTiger internal cache size.

Same problem.

Is it possible to fix this bug?

Yes, you need to set wiredTigerCacheSizeGB to an appropriate size, as
discussed above.

--wiredTigerCacheSizeGB

I have the same issue with you. I have add mongod --wiredTigerCacheSizeGB 5 in my RC mongo.yaml, but the docker memory used is 6GB and still increasing. Looks like the command did not work. BTW, the host node allocatable memory is 14.5G.

Try _--wiredTigerCacheSizeGB=5_ instead.

Try _--wiredTigerCacheSizeGB=5_ instead.

I have the same issue, and changing --wiredTigerCacheSizeGB=5 did not change anything

wiredTigerCacheSizeGB isn't the only memory that the MongoDB will use. MongoDB normally sets this to 50% of (RAM - 1 GB), but obviously doesn't understand cgroup limits that containers use, so you should do the same calculation with any container limits that you set (ie, if you set 5GB container limit, you should set wiredTigerCacheSizeGB to 2).

Defines the maximum size of the internal cache that WiredTiger will use for all data. The memory consumed by an index build (see maxIndexBuildMemoryUsageMegabytes) is separate from the WiredTiger cache memory.
[...]
Avoid increasing the WiredTiger internal cache size above its default value.
With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache.

- https://docs.mongodb.com/manual/reference/program/mongod/#cmdoption-mongod-wiredtigercachesizegb

what file do I make these changes in?

Was this page helpful?
0 / 5 - 0 ratings