mongodb 有没有限制mongodb内存使用的选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6861184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:11:49  来源:igfitidea点击:

Is there any option to limit mongodb memory usage?

mongodb

提问by kumar_88

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

我正在使用 Mongo-DBv1.8.1。我的服务器内存是 4GB,但 Mongo-DB 使用了超过 3GB。Mongo-DB 中是否有内存限制选项?

回答by mikkun

If you are running MongoDB 3.2or later version, you can limit the wiredTigercacheas mentioned above.

如果您运行的是MongoDB 3.2或更高版本,则可以如上所述限制wiredTiger缓存

In /etc/mongod.confadd the wiredTigerpart

/etc/mongod.conf添加wiredTiger部分

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

这会将缓存大小限制为1GB文档中的更多信息

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

这为我解决了这个问题,运行ubuntu 16.04mongoDB 3.2

PS: After changing the config, restart the mongo daemon.

PS:更改配置后,重启mongo daemon。

$ sudo service mongod restart

# check the status
$ sudo service mongod status

回答by Rajniprabha

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

从 3.2 开始,MongoDB 使用 WiredTiger 作为默认存储引擎。以前的版本使用 MMAPv1 作为默认存储引擎。

  • 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.
  • 通过 WiredTiger,MongoDB 使用 WiredTiger 内部缓存和文件系统缓存。
  • 在 MongoDB 3.2 中,默认情况下,WiredTiger 内部缓存将使用较大的值: RAM 的 60% 减去 1 GB 或 1 GB。
  • 对于高达 10 GB RAM 的系统,新的默认设置小于或等于 3.0 默认设置(对于 MongoDB 3.0,WiredTiger 内部缓存使用 1 GB 或已安装物理 RAM 的一半,以较大者为准)。对于 RAM 超过 10 GB 的系统,新的默认设置大于 3.0 设置。


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

限制wiredTriggered Cache 将以下行添加到.config 文件:

wiredTigerCacheSizeGB = 1

有线老虎缓存大小GB = 1

回答by Justin Jenkins

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 使用的缓存?



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 Cachingin the MongoDB docs.)

MongoDB 将(至少看起来)用掉大量可用内存,但它实际上让操作系统的 VMM 告诉它释放内存(请参阅MongoDB 文档中的缓存。)

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

您应该能够通过重新启动 MongoDB 来释放所有内存。

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

但是,在某种程度上,MongoDB 并没有真正“使用”内存。

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

例如来自 MongoDB 文档检查服务器内存使用情况...

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.

根据平台的不同,您可能会将映射文件视为进程中的内存,但这并不完全正确。Unix top 可能会为 mongod 显示比真正合适的更多内存。操作系统(特别是虚拟内存管理器,取决于操作系统)管理“内存映射文件”所在的内存。这个数字通常显示在像“free -lmt”这样的程序中。

它被称为“缓存”内存。

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 使用 LRU(最近最少使用)缓存算法来确定要释放哪些“页面”,您会在这两个问题中找到更多信息...

回答by rmc

You can limit mongod process usage using cgroups on Linux.

您可以在 Linux 上使用 cgroups 限制 mongod 进程的使用。

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

使用 cgroups,我们的任务可以通过几个简单的步骤来完成。

  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.

  1. 创建控制组:

    cgcreate -g memory:DBLimitedGroup 
    

    (确保您的系统上安装了 cgroups 二进制文件,请参阅您最喜欢的 Linux 发行版手册以了解如何执行此操作)

  2. 指定该组可用的内存量:

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

    此命令将内存限制为 16G(这限制了 malloc 分配和操作系统缓存的内存)

  3. 现在,删除已经留在缓存中的页面将是一个好主意:

    sync; echo 3 > /proc/sys/vm/drop_caches
    
  4. 最后将服务器分配给创建的控制组:

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

    这会将正在运行的 mongod 进程分配给仅受 16GB 内存限制的组。

source: Using Cgroups to Limit MySQL and MongoDB memory usage

来源:使用 Cgroups 限制 MySQL 和 MongoDB 内存使用

回答by Seamus Abshere

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

我认为您无法配置 MongoDB 使用多少内存,但这没关系(请阅读下文)。

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.

mongod 进程的虚拟内存大小和常驻大小看起来非常大。这是良性的:虚拟内存空间将仅大于打开和映射的数据文件的大小;驻留大小将根据机器上其他进程未使用的内存量而有所不同。

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

换句话说,如果其他程序需要,Mongo 会让它们使用内存。

回答by Kdeveloper

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

对于 Windows,似乎可以控制 MongoDB 使用的内存量,请参阅 Codeman 船长的本教程:

Limit MongoDB memory use on Windows without Virtualization

在没有虚拟化的情况下限制 Windows 上的 MongoDB 内存使用

回答by Chris Fulstow

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.

不是真的,有一些技巧可以限制内存,比如在 Windows 上你可以使用Windows 系统资源管理器 (WSRM),但通常 Mongo 在专用服务器上工作得最好,因为它可以自由使用内存而不会与其他系统发生太多争用。

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.

如果您真的需要限制内存,并且只有一台服务器,那么您最好的选择是虚拟化。

回答by brainsucker

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/

这可以通过 cgroups 完成,结合这两篇文章的知识: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

你可以在这里找到一个小的 shell 脚本,它将为 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'

回答by u6500087

mongod --wiredTigerCacheSizeGB 2 xx

回答by Walker Rowe

There is no reason to limit MongoDB cache as by default the mongod process will take 1/2 of the memory on the machine and no more. The default storage engine is WiredTiger. "With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache."

没有理由限制 MongoDB 缓存,因为默认情况下 mongod 进程将占用机器上 1/2 的内存,仅此而已。默认的存储引擎是 WiredTiger。“通过 WiredTiger,MongoDB 使用 WiredTiger 内部缓存和文件系统缓存。”

You are probably looking at top and assuming that Mongo is using all the memory on your machine. That is virtual memory. Use free -m:

您可能正在查看 top 并假设 Mongo 正在使用您机器上的所有内存。那就是虚拟内存。使用免费 -m:

              total        used        free      shared  buff/cache   available
Mem:           7982        1487        5601           8         893        6204
Swap:             0           0           0

Only when the available metric goes to zero is your computer swapping memory out to disk. In that case your database is too large for your machine. Add another mongodb instance to your cluster.

只有当可用指标变为零时,您的计算机才会将内存交换到磁盘。在这种情况下,您的数据库对于您的机器来说太大了。将另一个 mongodb 实例添加到您的集群。

Use these two commands in the mongod console to get information about how much virtual and physical memory Mongodb is using:

在 mongod 控制台中使用这两个命令来获取有关 Mongodb 正在使用多少虚拟和物理内存的信息:

var mem = db.serverStatus().tcmalloc;

mem.tcmalloc.formattedString

------------------------------------------------
MALLOC:      360509952 (  343.8 MiB) Bytes in use by application
MALLOC: +    477704192 (  455.6 MiB) Bytes in page heap freelist
MALLOC: +     33152680 (   31.6 MiB) Bytes in central cache freelist
MALLOC: +      2684032 (    2.6 MiB) Bytes in transfer cache freelist
MALLOC: +      3508952 (    3.3 MiB) Bytes in thread cache freelists
MALLOC: +      6349056 (    6.1 MiB) Bytes in malloc metadata
MALLOC:   ------------
MALLOC: =    883908864 (  843.0 MiB) Actual memory used (physical + swap)
MALLOC: +     33611776 (   32.1 MiB) Bytes released to OS (aka unmapped)
MALLOC:   ------------
MALLOC: =    917520640 (  875.0 MiB) Virtual address space used
MALLOC:
MALLOC:          26695              Spans in use
MALLOC:             22              Thread heaps in use
MALLOC:           4096              Tcmalloc page size