当前位置: 首页 > news >正文

mongodb 使用内存过大分析

os 分析

内存使用

ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head -10

swap 使用

for i in $(ls /proc | grep "^[0-9]" | awk '$0>100'); do awk '/Swap:/{a=a+$2}END{print '"$i"',a/1024"M"}' /proc/$i/smaps;done| sort -k2nr | head

mongodb 层面分析

查看tcmalloc内存分配器

data0data0:PRIMARY> db.version();
4.2.17data0data0:PRIMARY> db.serverStatus().tcmalloc.tcmalloc.formattedString
------------------------------------------------
MALLOC:    28062443328 (26762.4 MiB) Bytes in use by application
MALLOC: +  55441469440 (52873.1 MiB) Bytes in page heap freelist
MALLOC: +   8222845880 ( 7841.9 MiB) Bytes in central cache freelist
MALLOC: +        37632 (    0.0 MiB) Bytes in transfer cache freelist
MALLOC: +     20273672 (   19.3 MiB) Bytes in thread cache freelists
MALLOC: +    530448384 (  505.9 MiB) Bytes in malloc metadata
MALLOC:   ------------
MALLOC: =  92277518336 (88002.7 MiB) Actual memory used (physical + swap)
MALLOC: +   8146022400 ( 7768.7 MiB) Bytes released to OS (aka unmapped)
MALLOC:   ------------
MALLOC: = 100423540736 (95771.4 MiB) Virtual address space used
MALLOC:
MALLOC:        4213034              Spans in use
MALLOC:            118              Thread heaps in use
MALLOC:           4096              Tcmalloc page size
------------------------------------------------
Call ReleaseFreeMemory() to release freelist memory to the OS (via madvise()).
Bytes released to the OS take up virtual address space but no physical memory.

Bytes in use by application 对应的内存指 Mongod 节点实际消耗的内存
Bytes in page heap freelist 为未归还给操作系统的内存。

可以通过增大参数tcmallocReleaseRate提高释放内存速率

db.adminCommand( { getParameter: 1, tcmallocReleaseRate:1} )db.adminCommand( { setParameter: 1, tcmallocReleaseRate: 5.0 } )

进一步,如何希望立即释放内存,可以设置参数tcmallocAggressiveMemoryDecommit 为1

db.adminCommand( { getParameter: 1, tcmallocAggressiveMemoryDecommit:1} )db.adminCommand( { setParameter: 1, tcmallocAggressiveMemoryDecommit:1} )

查看wiredTiger cache

data0data0:PRIMARY> db.serverStatus().storageEngine;
{"name" : "wiredTiger","supportsCommittedReads" : true,"oldestRequiredTimestampForCrashRecovery" : Timestamp(1738804816, 1),"supportsPendingDrops" : true,"dropPendingIdents" : NumberLong(0),"supportsSnapshotReadConcern" : true,"readOnly" : false,"persistent" : true,"backupCursorOpen" : false
}

查看wiredTiger 内存当前使用大小

data0data0:PRIMARY> db.serverStatus().wiredTiger.cache

关注 bytes currently in the cache

{......"bytes belonging to page images in the cache":6511653424,"bytes belonging to the cache overflow table in the cache":65289,"bytes currently in the cache":8563140208,"bytes dirty in the cache cumulative":NumberLong("369249096605399"),......
}

查看 wiredTiger 内存配合的参数大小
单位字节

data0data0:PRIMARY> db.serverStatus().wiredTiger.cache["maximum bytes configured"]
1073741824

可以适当调小或者调大

data0data0:PRIMARY> db.adminCommand({setParameter: 1, "wiredTigerEngineRuntimeConfig": "cache_size=2GB" });

查看修改后的最新值

data0data0:PRIMARY> db.serverStatus().wiredTiger.cache["maximum bytes configured"];
2147483648

记得同步修改参数文件,避免实例重启后丢失修改值。

备注:
1、默认 WiredTiger 内部缓存大小为以下两者中的较大者:
(RAM 大小 - 1 GB)的 50%,或
256 MB.

https://www.mongodb.com/zh-cn/docs/v5.0/core/wiredtiger/#memory-use


http://www.mrgr.cn/news/89833.html

相关文章:

  • 学习笔记:机器学习中的数学原理(一)
  • 均方根层标准化(RMSNorm: Root Mean Square Layer Normalization)
  • 【从零开始系列】DeepSeek-R1:(本地部署使用)思维链推理大模型,开源的神!——Windows/Linux本地环境测试 + vLLM远程部署服务
  • k8s部署rabbitmq
  • 《Kettle实操案例一(全量/增量更新与邮件发送)》
  • 音频进阶学习十二——Z变换
  • 保姆级教程Docker部署KRaft模式的Kafka官方镜像
  • 【服务器知识】如何在linux系统上搭建一个nfs
  • 【Langchain学习笔记(二)】Langchain安装及使用示例
  • HIVE如何注册UDF函数
  • nodejs:express + js-mdict 网页查询英汉词典,能播放.spx 声音
  • Mac上搭建k8s环境——Minikube
  • 【非 root 用户下全局使用静态编译的 FFmpeg】
  • kafka服务端之延时操作实现原理
  • (一)C++的类与对象
  • Jmeter快速实操入门
  • docker安装es及分词器ik
  • 122,【6】buuctf web [护网杯2018] easy_tornado
  • 交叉编译工具链下载和使用
  • TaskBuilder项目实战:创建项目