how to release the caching which is used by Mongod

2019-01-08 14:47发布

问题:

Mongodb use the Memory Mapped File ,when I use a long time , I see the free memory has left less by command 'free -m' in ubuntu and the caching use a lot. Then kill the Mongodb the caching still cost a lot ? how can I release the caching ?

回答1:

MongoDB will (at least seem) to use up a lot of available memory, but it actually leaves it up to the OS's VMM to tell it to release the memory (see Caching in the MongoDB docs.)

You should be able to release any and all memory by restarting MongoDB.

However, to some extent MongoDB isn't really "using" the memory.

For example from the MongoDB docs Checking Server Memory Usage ...

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 uses the LRU (Least Recently Used) cache algorithm to determine which "pages" to release, you will find some more information in these two questions ...

  • MongoDB limit memory
  • MongoDB index/RAM relationship
  • Mongod start with memory limit (You can't.)


回答2:

Starting in 3.2, MongoDB uses the WiredTiger as the default storage engine. Previous versions used the MMAPv1 as the default storage engine.

With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache. In MongoDB 3.2, the WiredTiger internal cache, by default, will use the larger of either: 60% of RAM minus 1 GB, or 1 GB. For systems with up to 10 GB of RAM, the new default setting is less than or equal to the 3.0 default setting (For MongoDB 3.0, the WiredTiger internal cache uses either 1 GB or half of the installed physical RAM, whichever is larger). For systems with more than 10 GB of RAM, the new default setting is greater than the 3.0 setting.


to limit the wiredTriggered Cache Add following line to .config file :

wiredTigerCacheSizeGB = 1



回答3:

What was my issue:

Because the application deployed on machine from very long time and no historical data was removed which caused increase in size of database used with application. Mongodb occupies almost more than double the size of available data in RAM which was not leaving any free memory to run application.

Memory status before data deleted:

[root@ip-172-31-1-173 logs]# free -m total used free shared buff/cache available Mem: 15630 11462 146 732 4021 3207 Swap: 0 0 0

Memory status after data deleted:

[root@ip-172-31-1-173 mongo]# free -m total used free shared buff/cache available Mem: 15630 5849 5234 788 4545 8763 Swap: 0 0 0

With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache.

Changed in version 3.2: Starting in MongoDB 3.2, the WiredTiger internal cache, by default, will use the larger of either:

60% of RAM minus 1 GB, or
1 GB.

For systems with up to 10 GB of RAM, the new default setting is less than or equal to the 3.0 default setting (For MongoDB 3.0, the WiredTiger internal cache uses either 1 GB or half of the installed physical RAM, whichever is larger).

For systems with more than 10 GB of RAM, the new default setting is greater than the 3.0 setting.

Via the filesystem cache, MongoDB automatically uses all free memory that is not used by the WiredTiger cache or by other processes. Data in the filesystem cache is compressed.

You also need to repair database when you cleanup your historical data to maintain size of used cache.