mongodb 如何释放Mongodb使用的缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4468873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to release the caching which is used by Mongodb?
提问by Ted
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 ?
Mongodb 使用内存映射文件,当我使用很长时间时,我看到 ubuntu 中的命令“free -m”减少了可用内存,并且缓存使用了很多。那么杀掉Mongodb缓存还是要花不少钱的?我怎样才能释放缓存?
回答by Justin Jenkins
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 Cachingin the MongoDB docs.)
MongoDB 将(至少看起来)用掉大量可用内存,但它实际上让操作系统的 VMM 告诉它释放内存(请参阅MongoDB 文档中的缓存。)
You should be able to release any and all memory by restarting MongoDB.
您应该能够通过重新启动 MongoDB 来释放所有内存。
However, to some extent MongoDB isn't really "using" the memory.
但是,在某种程度上,MongoDB 并没有真正“使用”内存。
For example from the MongoDB docs Checking Server Memory Usage...
例如来自 MongoDB 文档检查服务器内存使用情况...
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.
根据平台的不同,您可能会将映射文件视为进程中的内存,但这并不完全正确。Unix top 可能会为 mongod 显示比真正合适的更多内存。操作系统(特别是虚拟内存管理器,取决于操作系统)管理“内存映射文件”所在的内存。这个数字通常显示在像“free -lmt”这样的程序中。
它被称为“缓存”内存。
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 使用 LRU(最近最少使用)缓存算法来确定要释放哪些“页面”,您会在这两个问题中找到更多信息...
回答by Rajniprabha
Starting in 3.2, MongoDB uses the WiredTiger as the default storage engine. Previous versions used the MMAPv1 as the default storage engine.
从 3.2 开始,MongoDB 使用 WiredTiger 作为默认存储引擎。以前的版本使用 MMAPv1 作为默认存储引擎。
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.
通过 WiredTiger,MongoDB 使用 WiredTiger 内部缓存和文件系统缓存。在 MongoDB 3.2 中,默认情况下,WiredTiger 内部缓存将使用较大的值:RAM 的 60% 减去 1 GB 或 1 GB。对于高达 10 GB RAM 的系统,新的默认设置小于或等于 3.0 默认设置(对于 MongoDB 3.0,WiredTiger 内部缓存使用 1 GB 或已安装物理 RAM 的一半,以较大者为准)。对于 RAM 超过 10 GB 的系统,新的默认设置大于 3.0 设置。
to limit the wiredTriggered Cache Add following line to .config file :
限制wiredTriggered Cache 将以下行添加到.config 文件:
wiredTigerCacheSizeGB = 1
有线老虎缓存大小GB = 1
回答by Armin Nikdel
We were experiencing a similar issue on a live/production server which was a member of a replica set, so restarting the process was not an option. Eventho it is not a good idea to flush RES memory (Residence memory), in our use case we had to do it, via following command:
我们在作为副本集成员的实时/生产服务器上遇到了类似的问题,因此无法重新启动该过程。即使刷新 RES 内存(驻留内存)并不是一个好主意,在我们的用例中,我们必须通过以下命令执行此操作:
yes | tr \n x | head -c $((1024*1024*1000)) | grep n
It create a process that will take over the RAM for 1GB, forcing mongodb to release memory for this process. This was for a system with 2GB of ram. If for example your system is using 20GB of RAM, you might need to run this for 10GB or 15GB:
它创建了一个进程,该进程将接管 1GB 的 RAM,迫使 mongodb 为该进程释放内存。这是针对具有 2GB 内存的系统。例如,如果您的系统使用 20GB 的 RAM,您可能需要为 10GB 或 15GB 运行它:
yes | tr \n x | head -c $((1024*1024*15000)) | grep n
回答by Chandan Gawri
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.
因为应用程序部署在机器上很长时间并且没有删除历史数据,导致应用程序使用的数据库大小增加。Mongodb 在 RAM 中几乎占据了可用数据大小的两倍多,而没有留下任何可用内存来运行应用程序。
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
[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
[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.
通过 WiredTiger,MongoDB 使用 WiredTiger 内部缓存和文件系统缓存。
Changed in version 3.2: Starting in MongoDB 3.2, the WiredTiger internal cache, by default, will use the larger of either:
在 3.2 版更改:从 MongoDB 3.2 开始,默认情况下 WiredTiger 内部缓存将使用较大者:
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).
对于高达 10 GB RAM 的系统,新的默认设置小于或等于 3.0 默认设置(对于 MongoDB 3.0,WiredTiger 内部缓存使用 1 GB 或已安装物理 RAM 的一半,以较大者为准)。
For systems with more than 10 GB of RAM, the new default setting is greater than the 3.0 setting.
对于 RAM 超过 10 GB 的系统,新的默认设置大于 3.0 设置。
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.
通过文件系统缓存,MongoDB 自动使用 WiredTiger 缓存或其他进程未使用的所有空闲内存。文件系统缓存中的数据被压缩。
You also need to repair database when you cleanup your historical data to maintain size of used cache.
当您清理历史数据以保持已用缓存的大小时,您还需要修复数据库。