1. MongoDB 限制内存
MongoDB 的内存占用是比较大的 ,一个32G的服务器,可能会被它占用掉 0.5 x (32-1)=15.5G 的内存,可以通过修改配置来修改默认行为,比如固定设置 3GB:
mongod.conf
storage: engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 3
启动 mongodb 的时候指定这个参数就好了。
官方参考:WiredTiger Storage Engine、 storage.wiredTiger Options。
从官方参考的 Memory Use 这一节可以看到内存使用规则:
Starting in MongoDB 3.4, the default WiredTiger internal cache size is the larger of either: 50% of (RAM - 1 GB), or 256 MB. For example, on a system with a total of 4GB of RAM the WiredTiger cache will use 1.5GB of RAM (0.5 * (4 GB - 1 GB) = 1.5 GB). Conversely, a system with a total of 1.25 GB of RAM will allocate 256 MB to the WiredTiger cache because that is more than half of the total RAM minus one gigabyte (0.5 * (1.25 GB - 1 GB) = 128 MB < 256 MB).