MongoDB 内存溢出,服务异常终止
MongoDB数据跑着跑着就崩了
一、前言
MongoDB数据库创建一张表,表行数:15142387
,表空间:982.24MB
,在MongoDB Compass执行查询语句。mongod.exe的占用内存一下子从200MB冲到5GB,系统可用内存在5GB左右,然后mongod.exe就down了
MongodDB运行时,缓存空间大小不受限制,系统内存有多少用多少,当存储数据量增加(数据查询速度变快),系统内存不够用,服务就异常中止。为避免这种情况出现,应该限制缓存上限。
二、配置参数
经过查询资料,在官网找到缓存参数 cacheSizeGB
storage.wiredTiger.engineConfig.cacheSizeGB
Type: float
Defines the maximum size of the internal cache that WiredTiger will use for all data. The memory consumed by an index build (see maxIndexBuildMemoryUsageMegabytes) is separate from the WiredTiger cache memory.
定义内部缓存最大大小,这个内部缓存是WiredTiger针对所有数据使用的。索引建立消耗的内存和WiredTiger缓存是分开的。
Values can range from 0.25 GB to 10000 GB.
内部缓存值的范围在0.25 GB ~ 10000 GB
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).
mongod.cfg
# where to write logging data.
systemLog:
destination: file
path: E:\abc\mongoDB\data\log\mongod.log
logAppend: true
# Where and how to store data.
storage:
dbPath: E:\abc\mongoDB\data\db
journal:
enabled: true
# engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 4
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
#security:
# authorization: enabled
再次执行相同的查询语句,虽然内存大小会快速上升,但不会超出设定的上限值(4GB)。但直接导致查询语句的速度降低,相同的查询语句,时间从 26ms
上升到 831ms
。
三、缓存问题
抄录官网上关于缓存的问答
地址链接:https://www.mongodb.com/docs/v5.0/faq/fundamentals/
Does MongoDB handle caching?
MongoDB能处理缓存吗?
Yes. MongoDB keeps most recently used data in RAM. If you have created indexes for your queries and your working data set fits in RAM, MongoDB serves all queries from memory.
可以。MongoDB能在内存中持久化大部分最近使用的数据。如果你已经为你的查询创建了索引,并且你的运行数据被载入到内存中,那么MongoDB会从内存中服务所有的查询。
MongoDB does not cache the query results in order to return the cached results for identical queries.
MongoDB不会为了返回相同的缓存结果而缓存查询结果
For more information on MongoDB and memory use, see WiredTiger and Memory Use.
标签:缓存,MongoDB,RAM,WiredTiger,内存,溢出,GB
From: https://www.cnblogs.com/caojun97/p/17046618.html