mongodb占用linux大量的内存问题
1.问题出现
某天我发现云服务器的内存使用达到了76%,我的乖乖这了得,我是8G的配置,上面没跑几个服务,这就很不正常。我立马用命令查看了下。
top
shift+m
好家伙,原来是mongodb‘独占鳌头’,占用了百分之四十多的内存。
于是我想着法儿来释放内存。
1.1 重启服务可以释放掉,这种方式估计没人会采用。
1.2 使用mongo内置命令也可以达到
mongo> use admin mongo> db.runCommand({closeAllDatabases:1})
1.3 也可以直接调整
shell> sysctl -w vm.drop_caches=1
等等。。
这些都是当时可以达到效果,但是过了一天,我又发现问题回到了原点,随着使用时间的增加,物理内存会逐步占用升高,治标不治本。
2.发现原因
于是我开始大量查阅在线资料。
有几篇博文讲的蛮全面(但是我又发现大量雷同文章出现)😂
这篇应该是原创,时间蛮早的。
最后我确定了我这种小白错误原因:
官网上写的很清楚:https://docs.mongodb.com/v3.4/core/wiredtiger/index.html
With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache. Starting in 3.4, the WiredTiger internal cache, by default, will use the larger of either: 50% of (RAM - 1 GB), or 256 MB. By default, WiredTiger uses Snappy block compression for all collections and prefix compression for all indexes. Compression defaults are configurable at a global level and can also be set on a per-collection and per-index basis during collection and index creation. Different representations are used for data in the WiredTiger internal cache versus the on-disk format: Data in the filesystem cache is the same as the on-disk format, including benefits of any compression for data files. The filesystem cache is used by the operating system to reduce disk I/O. Indexes loaded in the WiredTiger internal cache have a different data representation to the on-disk format, but can still take advantage of index prefix compression to reduce RAM usage. Index prefix compression deduplicates common prefixes from indexed fields. Collection data in the WiredTiger internal cache is uncompressed and uses a different representation from the on-disk format. Block compression can provide significant on-disk storage savings, but data must be uncompressed to be manipulated by the server. Via the filesystem cache, MongoDB automatically uses all free memory that is not used by the WiredTiger cache or by other processes. To adjust the size of the WiredTiger internal cache, seestorage.wiredTiger.engineConfig.cacheSizeGB and --wiredTigerCacheSizeGB. Avoid increasing the WiredTiger internal cache size above its default value
3.解决方式
原来是忽略这个原因,默认会使用50%的内存。
根据官方说的改改配置文件吧,加上这一段。
mongod.conf
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 2.5
2.5 代表2.5G,这这个看你的配置了和需求了。
持续观察后,发现它确实稳定到了我们的所设置的值。这就解决了,爽歪歪。
21:59:52
2021-03-22