Mongodb 一段时间后填满服务器内存
Mongodb fills the server memory after a while
我使用 flask 编写了 Python 程序,它从 MongoDB 读取了一些数据。但是在服务器上的程序 运行 几个小时后,它开始 MongoDB 填满服务器内存,直到所有服务器内存都用完。
Status of server resources when running the program
几个小时后,服务器资源状态如下
Status of server resources after a few hours
我用过的技术
- MongoDB 在 docker
- python 3.8
- 烧瓶 2.0.2
- pymongo 4.0.1
- 独角兽 20.1.0
Docker 文件
FROM python:3.8.7-buster
WORKDIR /flask_app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
ENV FLASK_APP=server.py
CMD ["gunicorn", "server:app", "-w", "2","--log-level" ,"DEBUG", "--threads", "2", "-b", "0.0.0.0:5000"]
其实这个问题是MongoDB缓存系统的问题。
要解决此问题,您必须限制 MongoDB 的 RAM 消耗量。
这不会降低MongoDB性能,对它影响不大,但确实解决了问题。
您需要将这些配置添加到 docker-campus 个配置中:
.......
deploy:
resources:
limits:
memory: 3G
reservations:
memory: 1G
command: --wiredTigerCacheSizeGB 0.25
.......
本例MongoDB内存消耗固定为5.2,您可以调整消耗。
祝你好运:)
我使用 flask 编写了 Python 程序,它从 MongoDB 读取了一些数据。但是在服务器上的程序 运行 几个小时后,它开始 MongoDB 填满服务器内存,直到所有服务器内存都用完。
Status of server resources when running the program
几个小时后,服务器资源状态如下
Status of server resources after a few hours
我用过的技术
- MongoDB 在 docker
- python 3.8
- 烧瓶 2.0.2
- pymongo 4.0.1
- 独角兽 20.1.0
Docker 文件
FROM python:3.8.7-buster
WORKDIR /flask_app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
ENV FLASK_APP=server.py
CMD ["gunicorn", "server:app", "-w", "2","--log-level" ,"DEBUG", "--threads", "2", "-b", "0.0.0.0:5000"]
其实这个问题是MongoDB缓存系统的问题。
要解决此问题,您必须限制 MongoDB 的 RAM 消耗量。
这不会降低MongoDB性能,对它影响不大,但确实解决了问题。
您需要将这些配置添加到 docker-campus 个配置中:
.......
deploy:
resources:
limits:
memory: 3G
reservations:
memory: 1G
command: --wiredTigerCacheSizeGB 0.25
.......
本例MongoDB内存消耗固定为5.2,您可以调整消耗。
祝你好运:)