2

MongoDB 到底要吃多少内存?

 2 years ago
source link: https://blog.lilydjwg.me/2016/4/11/mongodb-memory-usage.198819.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

MongoDB 到底要吃多少内存?

本文来自依云's Blog,转载请注明。

发现一只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 哄抢呢。

发送到 Kindle


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK