当前位置: 首页 > Linux

MongoDB4.2限制内存

时间:2023-04-06 21:10:22 Linux

先说结论吧。本文适配MongoDB4.2.1。不能通过MongoDB的配置文件来严格限制MongoDB占用的内存。建议将storage.wiredTiger.engineConfig.cacheSizeGB配置为预期最大内存的60%左右。学唐建发《MongoDB 高手课》推荐阅读张有东MongoDB是如何使用内存的?为什么内存满了?查看MongoDB内存使用情况mem>db.serverStatus().mem{"bits":64,//操作系统位"resident":15542,//常驻内存/物理内存(MB)"virtual":16760,//virtualmemory"supported":true//true表示支持额外的内存信息}tcmalloc>db.serverStatus().tcmalloc.tcmalloc.formattedString----------------------------------------------MALLOC:13431957064(12809.7MiB)applicationMALLOC:+1582014464(1508.7MiB))中的字节页堆freelistMALLOC:+1060879520(1011.7MiB)中央缓存中的字节freelistMALLOC:+2996608(2.9MiB)传输缓存中的字节freelistMALLOC:+7394200(7.1MiB)线程缓存中的字节freelistsMALLOC:+74448896(71.0MiB)字节mallocmetadataMALLOC:------------MALLOC:=16159690752(15411.1MiB)实际使用的内存(物理+交换)MALLOC:+5287936(5.0MiB)字节释放到操作系统(又名未映射)MALLOC:------------MALLOC:=16164978688(15416.1MiB)VirtualaddressspaceusedMALLOC:MALLOC:608155SpansinuseMALLOC:21ThreadheapsinuseMALLOC:4096Tcmallocpagesize--------------------------------------------------调用ReleaseFreeMemory()释放freelist内存给OS(通过madvise())。释放给OS的字节占用虚拟地址空间但是没有物理内存。红框显示的是大概的物理内存消耗storage.wiredTiger.engineConfig.cacheSizeGB从MongoDB3.4开始,该参数的默认值是以下两个中较大的一个:MBWindowswalkerwillmongodonWindowsServer2012R2。在cfg中设置storage.wiredTiger.engineConfig.cacheSizeGB为32后,在任务管理器中发现MongoDBServer占用的内存仍然达到45GB。相关配置如下:storage:dbPath:E:\MongoDB\Server\4.2\datajournal:enabled:trueengine:wiredTigerwiredTiger:engineConfig:cacheSizeGB:32所以想从系统配置上限制MongoDB的内存。WSRMWSRMWindowsSystemResourceManagerWindowsSystemResourceManager在WindowsServer2008中可以使用WSRM来限制MongoDB的内存。从WindowsServer2012开始,WSRM被弃用。类似的功能是由Hyper-V提供的。在Linux下,可以使用cgroups来限制MongoDB的内存使用。本文来自qbitsnap