Mongodb 4.2 memory limitation

Time:2020-2-26

Let’s start with conclusions.

This paper adapts mongodb 4.2.1
Cannot strictly limit the memory occupied by mongodb through its configuration file
Just configure the storage.wiredtiger.engineconfig.cachesizegb to occupy about 60% of the expected maximum memory
  • Recommended to learn Tang Jianfa’s mongodb master class
  • How to use memory in mongodb? Why is the memory full?

View mongodb memory usage

  • mem
> db.serverStatus().mem
{
    "Bits": 64, // operating system bits
    "Resident": 15542, // resident memory / physical memory (MB)
    "Virtual": 16760, // virtual memory
    "Supported": true // true indicates that additional memory information is supported
}
  • tcmalloc
> db.serverStatus().tcmalloc.tcmalloc.formattedString
------------------------------------------------
MALLOC:    13431957064 (12809.7 MiB) Bytes in use by application
MALLOC: +   1582014464 ( 1508.7 MiB) Bytes in page heap freelist
MALLOC: +   1060879520 ( 1011.7 MiB) Bytes in central cache freelist
MALLOC: +      2996608 (    2.9 MiB) Bytes in transfer cache freelist
MALLOC: +      7394200 (    7.1 MiB) Bytes in thread cache freelists
MALLOC: +     74448896 (   71.0 MiB) Bytes in malloc metadata
MALLOC:   ------------
MALLOC: =  16159690752 (15411.1 MiB) Actual memory used (physical + swap)
MALLOC: +      5287936 (    5.0 MiB) Bytes released to OS (aka unmapped)
MALLOC:   ------------
MALLOC: =  16164978688 (15416.1 MiB) Virtual address space used
MALLOC:
MALLOC:         608155              Spans in use
MALLOC:             21              Thread heaps in use
MALLOC:           4096              Tcmalloc page size
------------------------------------------------
Call ReleaseFreeMemory() to release freelist memory to the OS (via madvise()).
Bytes released to the OS take up virtual address space but no physical memory.
  • The red box shows the approximate physical memory consumption

Mongodb 4.2 memory limitation

storage.wiredTiger.engineConfig.cacheSizeGB

  • Starting from mongodb 3.4, the default value of this parameter is the larger of the following two:
(RAM - 1 GB)/2
256 MB

Windows

Walker sets the storage.wiredtiger.engineconfig.cachesizegb in mongod.cfg to 32 on Windows Server 2012 R2. It is found in the task manager that the memory occupied by mongodb server still reaches 45 GB. The relevant configurations are as follows:

storage:
  dbPath: E:\MongoDB\Server.2\data
  journal:
    enabled: true
  engine: wiredTiger
  wiredTiger:
    engineConfig:
         cacheSizeGB: 32

So I want to limit the memory of mongodb from the system configuration.

WSRM

WSRM
Windows System Resource Manager
Windows System Explorer

WSRM can be used to limit mongodb memory in Windows Server 2008. Since Windows Server 2012, WSRM has been deprecated. Similar functions are provided by hyper-v.

Linux

Cgroups can be used to limit the memory usage of mongodb under Linux

This article is from Walker snapshot