How Does MongoDB Consume and Use Memory?

Where Is MongoDB Used?

Storage Engine Cache

TCP Connection and Request Processing

net.ipv4.tcp_wmem = 8192  65536  16777216
net.ipv4.tcp_rmem = 8192 87380 16777216
tcp    ESTAB      0      0                       127.0.0.1:51601                                 127.0.0.1:personal-agent
skmem:(r0,rb2357478,t0,tb2626560,f0,w0,o0,bl0)
# cat /proc/$pid/smaps7f563a6b2000-7f563b0b2000 rw-p 00000000 00:00 0
Size: 10240 kB
Rss: 12 kB
Pss: 12 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 12 kB
Referenced: 12 kB
Anonymous: 12 kB
AnonHugePages: 0 kB
Swap: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
mymongo:PRIMARY> db.serverStatus().tcmalloc
{
"generic" : {
"current_allocated_bytes" : NumberLong("2545084352"),
"heap_size" : NumberLong("2687029248")
},
"tcmalloc" : {
"pageheap_free_bytes" : 34529280,
"pageheap_unmapped_bytes" : 21135360,
"max_total_thread_cache_bytes" : NumberLong(1073741824),
"current_total_thread_cache_bytes" : 1057800,
"total_free_bytes" : 86280256,
"central_cache_free_bytes" : 84363448,
"transfer_cache_free_bytes" : 859008,
"thread_cache_free_bytes" : 1057800,
"aggressive_memory_decommit" : 0,
...
}
}

How Can You Control Memory Usage?

Configuring the WiredTiger cacheSizeGB Rationally

Controlling the Number of Concurrent Connections

Should I Set SWAP or Not?

For the WiredTiger storage engine, given sufficient memory pressure, WiredTiger may store data in swap space.Assign swap space for your systems. Allocating swap space can avoid issues with memory contention and can prevent the OOM Killer on Linux systems from killing mongod.

Other Features

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store