mongodbdocker

Why does mongodb in a docker consume so much memory?


May I ask for some help about mongodb memory usage? Thanks in advance!

I have a production mongodb instance running in the docker compose. The mongodb status is as follows,

> db.serverStatus().tcmalloc.tcmalloc.formattedString
------------------------------------------------
MALLOC:      612502384 (  584.1 MiB) Bytes in use by application
MALLOC: +    936054784 (  892.7 MiB) Bytes in page heap freelist
MALLOC: +     49701472 (   47.4 MiB) Bytes in central cache freelist
MALLOC: +      5157248 (    4.9 MiB) Bytes in transfer cache freelist
MALLOC: +     11567280 (   11.0 MiB) Bytes in thread cache freelists
MALLOC: +      9568256 (    9.1 MiB) Bytes in malloc metadata
MALLOC:   ------------
MALLOC: =   1624551424 ( 1549.3 MiB) Actual memory used (physical + swap)
MALLOC: +    311873536 (  297.4 MiB) Bytes released to OS (aka unmapped)
MALLOC:   ------------
MALLOC: =   1936424960 ( 1846.7 MiB) Virtual address space used
MALLOC:
MALLOC:          29704              Spans in use
MALLOC:            274              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.

IIUC, it uses 1846.7 MiB memory. But when I check the docker status,

# docker stats --no-stream
CONTAINER ID   NAME                     CPU %     MEM USAGE / LIMIT     MEM %     NET I/O          BLOCK I/O        PIDS
56b9bd33c533   slip-backend-web-1       0.00%     12.32MiB / 7.447GiB   0.16%     142MB / 183MB    0B / 0B          3
3f2c6a129d91   slip-backend-backend-1   0.08%     161.1MiB / 7.447GiB   2.11%     239MB / 210MB    0B / 0B          5
6afbcbf0dddc   slip-backend-mongo-1     0.61%     4.212GiB / 7.447GiB   56.57%    45.9GB / 202GB   1.88GB / 798GB   650

I found it consums 4.212GiB memory.

Could you give me a hand to figure out why there is a memory difference and how to avoid it?


From the following command, we can see that the mongodb container has been running for over 2 months, not sure if it's related.

# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED       STATUS       PORTS                                                                                                                 NAMES
56b9bd33c533   nginx                  "/docker-entrypoint.…"   2 hours ago   Up 2 hours   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp, 0.0.0.0:8123->8133/tcp, :::8123->8133/tcp   slip-backend-web-1
3f2c6a129d91   slip-backend-backend   "python3 server.py"      2 hours ago   Up 2 hours                                                                                                                         slip-backend-backend-1
6afbcbf0dddc   mongo                  "docker-entrypoint.s…"   8 weeks ago   Up 8 weeks   0.0.0.0:321->321/tcp, :::321->321/tcp, 27017/tcp

Solution

  • I've found a temporary workaround by restarting the MongoDB container using the command

    docker compose restart mongo
    

    and then,

    # docker stats --no-stream
    CONTAINER ID   NAME                     CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
    56b9bd33c533   slip-backend-web-1       0.00%     14.61MiB / 7.447GiB   0.19%     191MB / 247MB     0B / 0B           3
    3f2c6a129d91   slip-backend-backend-1   0.12%     185MiB / 7.447GiB     2.43%     318MB / 283MB     0B / 0B           5
    6afbcbf0dddc   slip-backend-mongo-1     0.72%     243.3MiB / 7.447GiB   3.19%     8.65MB / 9.71MB   23.9MB / 38.6MB   38
    

    While I haven't yet identified the root cause, it appears that over time, the memory used by the MongoDB container isn't being properly released, leading to increasing memory consumption. It seems like a memory leak or a problem with memory management within the container.

    Regardless, I believe this is a valuable issue to document, and I hope this workaround helps others who encounter similar problems.