MongoDB 如何使用内存? 为什么内存满了?( 七 )

MongoDB driver 在连接 mongod 时 , 会维护一个连接池(通常默认100) , 当有大量的客户端同时访问同一个mongod时 , 就需要考虑减小每个客户端连接池的大小 。 mongod 可以通过配置 net.maxIncomingConnections 配置项来限制最大的并发连接数量 , 防止数据库压力过载 。

是否应该配置 SWAP

官方文档上的建议如下 , 意思是配置一下swap , 避免mongod因为内存使用太多而OOM 。

For the WiredTiger storage engine given sufficient memory pressure WiredTiger may store data in swap space.

Assign swap space for your systems. Allocating swap space can avoid issues with memory contention and can prevent the OOM Killer on Linux systems from killing mongod.

开启 SWAP 与否各有优劣 , SWAP开启 , 在内存压力大的时候 , 会利用SWAP磁盘空间来缓解内存压力 , 此时整个数据库服务会变慢 , 但具体变慢到什么程度是不可控的 。 不开启SWAP , 当整体内存超过机器内存上线时就会触发OOM killer把进程干掉 , 实际上是在告诉你 , 可能需要扩展一下内存资源或是优化对数据库的访问了 。

推荐阅读