Docker 容器的内存消耗从不减少(或者是吗?)

标签 docker memory

我们在单个 docker 主机上运行多个容器,主要用于运行 R 和 Python 应用程序进行数据分析。因此,当我将一个大表加载到其中一个容器中时,它在 docker 主机上的内存占用量会增加。

但是,当我关闭 Jupyter Notebook 或 R session 时,容器的内存占用似乎在主机上保持不变。看来docker容器的内存消耗只能上升,不能下降。

所以我知道 Linux 通常会占用其他应用程序不需要的内存(缓存的东西)。但是,对于 Docker 容器,这是如何处理的?从单个容器的角度来看,有很多内存(我们不想限制容器可用的内存),即使在这个特定容器内不需要它,它也会在容器中保持“占用”状态,并且因此其他容器无法访问。并且主机不知道这 block 内存是真的需要还是仅仅用于缓存。

那么这个是怎么处理的呢?我可以想象这样一种情况,几个人启动了他们加载或生成大数据集的容器,但这只是暂时的,现在主机的内存都被占用了,因为内存没有释放。

我很确定这不是它的工作原理,所以有人可以向我解释一下吗?

非常感谢,

恩诺

最佳答案

在Docker文档中,在资源限制下,有关于限制容器内存的解释。运行容器时,不会根据容器中运行的进程释放内存。文档解释了主机系统如何管理 memory :

It is important not to allow a running container to consume too much of the host machine’s memory. On Linux hosts, if the kernel detects that there is not enough memory to perform important system functions, it throws an OOME, or Out Of Memory Exception, and starts killing processes to free up memory. Any process is subject to killing, including Docker and other important applications. This can effectively bring the entire system down if the wrong process is killed.

Docker attempts to mitigate these risks by adjusting the OOM priority on the Docker daemon so that it is less likely to be killed than other processes on the system...

Docker 容器可以使用内存,但会被 Docker 守护进程阻止使主机系统崩溃。分配给 Docker 容器的内存也可以是 limited :

Docker can enforce hard memory limits, which allow the container to use no more than a given amount of user or system memory, or soft limits, which allow the container to use as much memory as it needs unless certain conditions are met, such as when the kernel detects low memory or contention on the host machine.

我们不想限制容器的内存,但有一些选项可以这样做,比如 --memory=<value> , --memory-swap , 和 --memory-reservation .所以不,主机无法释放正在运行的容器的内存,但它确实可以防止所有内存被占用并使内核可能终止关键系统进程的风险。

请原谅格式化。希望这可以帮助;我还链接了相关文档。此外,并不完全相关,但也许您可以查看有关在容器中使用 Java 应用程序的信息:
Why the docker container memory usage doesn't decrease?

关于Docker 容器的内存消耗从不减少(或者是吗?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48686599/

相关文章:

docker - Docker-swarm错误:只读文件系统

c - 表示已知大小变量的表达式结果的最少位数?

c - 如何在不调用外部程序的情况下测量进程的内存使用情况

docker - 如何使用docker-compose为容器设置固定IP地址?

docker - 为什么我的容器化 Selenium 应用程序仅在 AWS Lambda 中失败?

C++方法参数按引用传递-内存问题

c++ - 为什么我会出现内存泄漏?

c# - 计算十亿元素列表中唯一元素的最快方法是什么?

mongodb - 无法使用登录名/密码登录到 docker-compose 容器中的 mongoDB

docker - 如何在 docker-compose 中配置网络?