Understanding why MongoDB uses more RAM than the allowed wiredTigerCacheSizeGB

All we need is an easy explanation of the problem, so here it is.

My mongod command

mongod --wiredTigerCacheSizeGB 5

In practice, I’m getting mongo instance to use even up to 8.6 GB (on a VM with 50 GB RAM)

Indexes are using about 100 MB (checked with db.stats())

tcmalloc information:

MALLOC:     3776745552 ( 3601.8 MiB) Bytes in use by application
MALLOC: +   4744544256 ( 4524.8 MiB) Bytes in page heap freelist
MALLOC: +     20067616 (   19.1 MiB) Bytes in central cache freelist
MALLOC: +      3584128 (    3.4 MiB) Bytes in transfer cache freelist
MALLOC: +     12470800 (   11.9 MiB) Bytes in thread cache freelists
MALLOC: +     22544384 (   21.5 MiB) Bytes in malloc metadata
MALLOC:   ------------
MALLOC: =   8579956736 ( 8182.5 MiB) Actual memory used (physical + swap)
MALLOC: +     29933568 (   28.5 MiB) Bytes released to OS (aka unmapped)
MALLOC:   ------------
MALLOC: =   8609890304 ( 8211.0 MiB) Virtual address space used
MALLOC:          21650              Spans in use
MALLOC:             59              Thread heaps in use
MALLOC:           4096              Tcmalloc page size

The server had been running for 3 days.

How can I tell why mongo is consuming more memory than the allowed cache size? Is there any way to manually free up the "page heap freelist"?

The command output is recommending Call ReleaseFreeMemory() to release freelist memory to the OS, but I don’t think I can actually do that out of the process.

MongoDB version is 4.4.1, I’m using mongo:4.4.1-bionic docker image.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The maximum cache size is just that – a limit on the size of the cache used by the underlying storage engine.

If you run queries, sort things, make connections, etc, those all require memory outside of the cache.

MongoDB uses tcmalloc for memory management, which doesn’t immediately release memory back to the OS when the program calls free.

You can adjust how tcmalloc releases memory with the tcmallocAggressiveMemoryDecommit server parameter added here:

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply