是否有任何限制mongodb内存使用的选项?

Is there any option to limit mongodb memory usage?

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


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

/etc/mongod.conf中添加wiredTiger部分

1
2
3
4
5
6
7
8
9
10
...
# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
        cacheSizeGB: 1
...

这会将缓存大小限制为1GB,有关更多信息,请参阅Doc

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

PS:更改配置后,重新启动mongo守护程序。

1
2
3
4
$ sudo service mongod restart

# check the status
$ sudo service mongod status


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

  • 通过WiredTiger,MongoDB可以利用WiredTiger内部缓存和文件系统缓存。
  • 在MongoDB 3.2中,默认情况下,WiredTiger内部缓存将使用以下两者中的较大者:
    60%的RAM减去1 GB,或者
    1 GB。
  • 对于具有10 GB RAM的系统,新的默认设置小于或等于3.0默认设置(对于MongoDB 3.0,WiredTiger内部缓存使用的是1 GB或已安装物理RAM的一半,以较大者为准)。
    对于具有10 GB以上RAM的系统,新的默认设置大于3.0设置。

限制wireTriggered缓存在.config文件中添加以下行:

wiredTigerCacheSizeGB = 1


这个问题已经问了几次...

请参阅此相关问题/答案(在下面引用)...如何释放Mongodb使用的缓存?

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

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

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

例如,来自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.

MongoDB使用LRU(最近最少使用)缓存算法来确定要释放的"页面",您将在这两个问题中找到更多信息...

  • MongoDB限制内存
  • MongoDB索引/ RAM关系
  • Mongod从内存限制开始(您不能。)

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

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

  • 创建控制组:

    1
    cgcreate -g memory:DBLimitedGroup

    (确保在系统上安装了cgroups二进制文件,有关该操作,请查阅您最喜欢的Linux发行手册)

  • 指定该组有多少可用内存:

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

    此命令将内存限制为16G(这很好,这限制了malloc分配和OS缓存的内存)

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

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

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

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

  • 来源:使用Cgroup限制MySQL和MongoDB的内存使用


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

    引用官方消息:

    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.

    换句话说,Mongo将允许其他程序在需要时使用内存。


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

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


    1
    mongod --wiredTigerCacheSizeGB 2 xx


    通过结合以下两篇文章的知识,可以使用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/

    您可以在此处找到一个小的Shell脚本,该脚本将为Ubuntu 14.04创建配置文件和初始化文件:
    http://brainsuckerna.blogspot.com.by/2016/05/limiting-mongodb-memory-usage-with.html

    就像这样:

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

    并不是真的,有一些技巧可以限制内存,例如在Windows上,您可以使用Windows系统资源管理器(WSRM),但是通常,Mongo在专用服务器上可以自由使用内存而不会与其他系统发生过多争用,因此在专用服务器上效果最佳。

    尽管操作系统将尝试根据需要将内存分配给其他进程,但实际上,如果其他系统也具有很高的内存要求,则可能导致性能问题。

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


    没有理由限制MongoDB缓存,因为默认情况下,mongod进程将占用计算机内存的1/2,而不会占用更多内存。默认存储引擎为WiredTiger。"通过WiredTiger,MongoDB同时利用了WiredTiger内部缓存和文件系统缓存。"

    您可能正在寻找顶部,并假设Mongo正在使用计算机上的所有内存。那就是虚拟内存。使用免费的-m:

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

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

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    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

    如果您使用的是内存不足的计算机,并且想要以MB为单位而不是整数GB来配置wiredTigerCache,请添加到投票最高的答案中,请使用-

    1
    2
    3
    4
    storage:
     wiredTiger:
      engineConfig:
        configString : cache_size=345M

    来源-https://jira.mongodb.org/browse/SERVER-22274


    这对我在一个AWS实例上起作用,至少可以清除mongo在使用的缓存内存。之后,您可以看到您的设置如何生效。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ubuntu@hongse:~$ free -m
                 total       used       free     shared    buffers     cached
    Mem:          3952       3667        284          0        617        514
    -/+ buffers/cache:       2535       1416
    Swap:            0          0          0
    ubuntu@hongse:~$ sudo su
    root@hongse:/home/ubuntu# sync; echo 3 > /proc/sys/vm/drop_caches
    root@hongse:/home/ubuntu# free -m
                 total       used       free     shared    buffers     cached
    Mem:          3952       2269       1682          0          1         42
    -/+ buffers/cache:       2225       1726
    Swap:            0          0          0

    您可以限制的一件事是mongodb在构建索引时使用的内存量。使用maxIndexBuildMemoryUsageMegabytes设置进行设置。下面是其设置示例:

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