暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

mongodb占用内存非常高

原创 zayki 2023-02-14
2416

mongodb占用内存非常高,这是因为官方为了提升存储的效率,设计就这么设计的。

但是大部分的个人开发者所购买的服务器内存并没有那么大,所以,我们需要配置下MongoDB的内存缓存大小,不然mongodb会占用非常多。

官方的配置缓存项处文档是这么解释的:

WiredTiger Options
–wiredTigerCacheSizeGB number
New in version 3.0.

Defines the maximum size of the internal cache that WiredTiger will use for all data.

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.
mongodb会尽可能的把所有的数据都缓存,以便提高效率。

以mongodb 3.2为例,WiredTiger内部缓存,默认会用掉

60% * 内存 - 1GB
1GB
当你的内存大于1GB,mongodb会用掉 内存的60% - 1GB 的内存作为缓存;

当你的内存小于1GB,mongodb会直接用掉1GB。

另外,MongoDB 3.4与3.2也是有区别的,MongoDB 3.4该配置项为:

storage.wiredTiger.engineConfig.cacheSizeGB

Type: float

The maximum size of the internal cache that WiredTiger will use for all data.

Changed in version 3.4: Values can range from 256MB to 10TB and can be a float. In addition, the default value has also changed.

Starting in 3.4, the WiredTiger internal cache, by default, will use the larger of either:

50% of RAM minus 1 GB, or
256 MB.

这样显然很不合理,对于大部分的个人开发,内存是宝贵的。所以,我们需要配置为MB。

配置项参考此处配置:WiredTiger cache size is only configurable in whole gigabytes.

下面是修改后的配置:/etc/mongod.conf

Where and how to store data.

storage:
dbPath: /var/lib/mongo
#dbPath: /mongodata
journal:
enabled: true

engine:

mmapv1:
smallFiles: true
wiredTiger:
engineConfig:
configString : cache_size=512M
其实重点就是下面一项,配置之后,重启mongodb生效:

wiredTiger:
engineConfig:
configString : cache_size=512M

发现一只32G内存的服务器,上边跑了几个 sharding 模式的 mongod,把内存吃到只剩下4G,8G swap 更是丁点不剩。

我见过吃内存的 mongod,可没见过大胃口的 mongod 啊。不过以前我也没怎么见到在这么大内存的机器上跑的 mongod。不过不管如何,把 swap 全吃掉总归是不对的。

于是翻了翻 mongodb 源码,发现出现这种情况还真是机器的配置的问题。代码里有这么一段(在 GitHub 上的位置):

wiredtiger_init.cpp

if (cacheSizeGB == 0) {
// Since the user didn’t provide a cache size, choose a reasonable default value.
// We want to reserve 1GB for the system and binaries, but it’s not bad to
// leave a fair amount left over for pagecache since that’s compressed storage.
ProcessInfo pi;
double memSizeMB = pi.getMemSizeMB();
if (memSizeMB > 0) {
double cacheMB = (memSizeMB - 1024) * 0.6;
cacheSizeGB = static_cast<size_t>(cacheMB / 1024);
if (cacheSizeGB < 1)
cacheSizeGB = 1;
}
}
大概这就是决定它自己要用多少内存的代码了。先留出1G,然后再留出40%,剩下的能吃就吃!于是,好几只 mongod 开始抢食了!默认vm.swappiness=60的内核看到内存快用完了,于是开始往 swap 挪。结果造成内核挪多少,mongod 吃多少……

这种情况在机器内存少的时候没有出现,大概是因为内存少的时候,mongod 留出的比例比较高,内核就没那么卖力地把数据往 swap 上挪了。而且这次是好几只 mongod 哄抢呢。

[root@zlf-mogodb-pro-app ~]# vmstat 3
procs -----------memory---------- —swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
34 12 7284952 143488 352 96452 0 0 1 32 0 0 0 0 100 0 0
7 59 7287976 136376 240 97812 30464 21940 50715 23497 50447 67820 9 7 0 83 0
11 54 7274824 135980 236 81900 37103 21549 50636 23397 52595 71067 16 8 4 72 0
39 50 7297968 142496 308 94108 27047 24605 50592 24663 55431 74703 10 8 0 82 0
8 38 7307016 137844 264 73728 33715 25228 44732 27360 63486 88237 12 9 0 79 0
2 32 7322716 144948 288 83712 24529 21755 41936 23739 66441 99917 7 9 0 84 0
10 35 7324924 147228 260 69596 37961 26384 65865 26456 85899 131963 7 9 4 80 0
7 27 7326072 135724 220 73888 31565 22651 62453 23615 73028 112568 6 10 0 84 0
8 26 7337232 136456 212 87980 27711 20979 55908 21215 73208 116429 5 10 0 85 0
28 22 7333816 135928 212 92052 31015 20084 62657 21480 72619 115027 6 10 0 84 0
5 21 7339004 157784 328 67140 26240 19724 75627 22665 78647 126346 6 10 7 76 0
39 21 7348012 168364 216 76880 17187 14933 111107 15648 66716 113048 5 16 2 78 0
^C
[root@zlf-mogodb-pro-app ~]# iostat 3
Linux 3.10.0-1160.6.1.el7.x86_64 (zlf-mogodb-pro-app) 02/14/2023 x86_64 (4 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle
0.17 0.00 0.04 0.02 0.00 99.76

Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 7.45 3.64 128.50 199569282 7046357100

avg-cpu: %user %nice %system %iowait %steal %idle
16.33 0.00 8.08 75.60 0.00 0.00

Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 5681.00 44110.67 32813.33 132332 98440

avg-cpu: %user %nice %system %iowait %steal %idle
14.12 0.00 8.33 76.87 0.00 0.68

Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 5425.00 39673.33 21750.67 119020 65252

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论