Here's the answer of coolljt0725, one of the contributors, answering why a RES column in top output shows something different, than docker stats (I'm just gonna quote him as is):如果我正确理解,则是从容器的内存cgroup中完全读取docker stats中的内存使用情况,您可以看到该值与从cat/sys/fs/cgroup/memory/docker/665e99f8b760c0300f10d3d9b35b1a5e5fdcf1b7e4a0100969memory.usage_in_bytes,该限制也是创建容器时由-m设置的内存cgroup限制.RES和内存cgroup的统计信息不同,RES没有考虑缓存,但内存cgroup却考虑到了这,这就是为什么docker stats中的MEM USAGE远比top中的RES更重要 If I understand correctly, the memory usage in docker stats is exactly read from containers's memory cgroup, you can see the value is the same with 490270720 which you read from cat /sys/fs/cgroup/memory/docker/665e99f8b760c0300f10d3d9b35b1a5e5fdcf1b7e4a0e27c1b6ff100981d9a69/memory.usage_in_bytes, and the limit is also the memory cgroup limit which is set by -m when you create container. The statistics of RES and memory cgroup are different, the RES does not take caches into account, but the memory cgroup does, that's why MEM USAGE in docker stats is much more than RES in top 此处用户的建议实际上可以帮助您查看实际内存消耗:What a user suggested here might actually help you to see the real memory consumption:尝试设置 docker run --memory 的参数,然后检查/sys/fs/cgroup/memory/docker/<container_id>/memory.usage_in_bytes 应该是对的. Try set the param of docker run --memory,then check your /sys/fs/cgroup/memory/docker/<container_id>/memory.usage_in_bytes It should be right. -内存或 -m 在此处: -m ,-memory =" -内存限制(格式:< number> [< unit>] ).数字是一个正整数.单位可以是 b , k , m 或 g 之一.最小为 4M . -m, --memory="" - Memory limit (format: <number>[<unit>]). Number is a positive integer. Unit can be one of b, k, m, or g. Minimum is 4M.现在如何避免不必要的内存消耗.就像您发布的一样,在PHP中取消链接文件并不需要立即删除内存缓存.相反,可以在特权模式下运行Docker容器(带有-privileged 标志),然后可以调用 echo 3>./proc/sys/vm/drop_caches 或 sync&&sysctl -w vm.drop_caches = 3 定期清除内存页面缓存.And now how to avoid the unnecessary memory consumption. Just as you posted, unlinking a file in PHP does not necessary drop memory cache immediately. Instead, running the Docker container in privileged mode (with --privileged flag) it is then possible to call echo 3 > /proc/sys/vm/drop_caches or sync && sysctl -w vm.drop_caches=3 periodcally to clear the memory pagecache.另外,使用 fopen('php://temp','w +')并将文件临时存储在内存中可以避免整个问题.And as a bonus, using fopen('php://temp', 'w+') and storing the file temporary in memory avoids the entire issue. 这篇关于使用PHP创建和删除文件后,Docker无法释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-27 13:24
查看更多