Is there any option to limit mongodb memory usage?

2019-01-06 12:24发布

问题:

I am using Mongo-DBv1.8.1. My server memory is 4GB but Mongo-DB is utilizing more than 3GB. Is there memory limitation option in Mongo-DB?.

回答1:

This question has been asked a couple times ...

See this related question/answer (quoted below) ... how to release the caching which is used by Mongodb?


MongoDB will (at least seem) to use up a lot of available memory, but it actually leaves it up to the OS's VMM to tell it to release the memory (see Caching in the MongoDB docs.)

You should be able to release any and all memory by restarting MongoDB.

However, to some extent MongoDB isn't really "using" the memory.

For example from the MongoDB docs Checking Server Memory Usage ...

Depending on the platform you may see the mapped files as memory in the process, but this is not strictly correct. Unix top may show way more memory for mongod than is really appropriate. The Operating System (the virtual memory manager specifically, depending on OS) manages the memory where the "Memory Mapped Files" reside. This number is usually shown in a program like "free -lmt".

It is called "cached" memory.

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 ...

  • MongoDB limit memory
  • MongoDB index/RAM relationship
  • Mongod start with memory limit (You can't.)


回答2:

Starting in 3.2, MongoDB uses the WiredTiger as the default storage engine. Previous versions used the MMAPv1 as the default storage engine.

  • With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache.
  • In MongoDB 3.2, the WiredTiger internal cache, by default, will use the larger of either: 60% of RAM minus 1 GB, or 1 GB.
  • For systems with up to 10 GB of RAM, the new default setting is less than or equal to the 3.0 default setting (For MongoDB 3.0, the WiredTiger internal cache uses either 1 GB or half of the installed physical RAM, whichever is larger). For systems with more than 10 GB of RAM, the new default setting is greater than the 3.0 setting.

to limit the wiredTriggered Cache Add following line to .config file :

wiredTigerCacheSizeGB = 1



回答3:

If you are running mongodb 3.2+ you can limit the wiredTiger cache as mentioned above.

In /etc/mongod.conf add the wiredTigre part

...
# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
        cacheSizeGB: 1
...

This will limit the cache size to 1Gb, more info in docks

This solved the issue for me, running ubuntu 16.04 and mongoDB 3.2



回答4:

You can limit mongod process usage using cgroups on Linux.

Using cgroups, our task can be accomplished in a few easy steps.

  1. Create control group:

    cgcreate -g memory:DBLimitedGroup 
    

    (make sure that cgroups binaries installed on your system, consult your favorite Linux distribution manual for how to do that)

  2. Specify how much memory will be available for this group:

    echo 16G > /sys/fs/cgroup/memory/DBLimitedGroup/memory.limit_in_bytes
    

    This command limits memory to 16G (good thing this limits the memory for both malloc allocations and OS cache)

  3. Now, it will be a good idea to drop pages already stayed in cache:

    sync; echo 3 > /proc/sys/vm/drop_caches
    
  4. And finally assign a server to created control group:

    cgclassify -g memory:DBLimitedGroup \`pidof mongod\`
    

    This will assign a running mongod process to a group limited by only 16GB memory.

source: Using Cgroups to Limit MySQL and MongoDB memory usage



回答5:

I don't think you can configure how much memory MongoDB uses, but that's OK (read below).

To quote from the official source:

Virtual memory size and resident size will appear to be very large for the mongod process. This is benign: virtual memory space will be just larger than the size of the datafiles open and mapped; resident size will vary depending on the amount of memory not used by other processes on the machine.

In other words, Mongo will let other programs use memory if they ask for it.



回答6:

For Windows it seems possible to control the amount of memory MongoDB uses, see this tutorial at Captain Codeman:

Limit MongoDB memory use on Windows without Virtualization



回答7:

Not really, there are a couple of tricks to limit memory, like on Windows you can use the Windows System Resource Manager (WSRM), but generally Mongo works best on a dedicated server when it's free to use memory without much contention with other systems.

Although the operating system will try to allocate memory to other processes as they need it, in practice this can lead to performance issues if other systems have high memory requirements too.

If you really need to limit memory, and only have a single server, then your best bet is virtualization.



回答8:

This can be done with cgroups, by combining knowledge from these two articles: https://www.percona.com/blog/2015/07/01/using-cgroups-to-limit-mysql-and-mongodb-memory-usage/
http://frank2.net/cgroups-ubuntu-14-04/

You can find here a small shell script which will create config and init files for Ubuntu 14.04: http://brainsuckerna.blogspot.com.by/2016/05/limiting-mongodb-memory-usage-with.html

Just like that:

sudo bash -c 'curl -o- http://brains.by/misc/mongodb_memory_limit_ubuntu1404.sh | bash'


回答9:

One thing you can limit is the amount of memory mongodb uses while building indexes. This is set using the maxIndexBuildMemoryUsageMegabytes setting. An example of how its set is below:

mongo --eval "db.adminCommand( { setParameter: 1, maxIndexBuildMemoryUsageMegabytes: 70000 } )"



回答10:

mongod --wiredTigerCacheSizeGB 2 xx


标签: mongodb