简体   繁体   English

写入已安装目录中的文件时,docker容器会不断增加内存使用量

[英]docker container keep increasing memory usage when writing into a file in a mounted directory

I'm facing an issue that memory used by a container keep increasing when an app inside the container writing log into a file in a mounted directory. 我面临一个问题,当容器内的应用程序将日志写入已安装目录中的文件时,容器使用的内存会不断增加。

I'd expect memory usage doesn't increase by this. 我希望内存使用量不会因此增加。 Does anyone have idea why it increases ? 有谁知道为什么它增加? Thank you !! 谢谢 !!

Here is what I did: 这是我所做的:

  1. Write an app which just writes "hello world" into "/home/mylog/test.txt". 编写一个仅将“ hello world”写入“ /home/mylog/test.txt”的应用程序。

     func main(){ file, _ := os.OpenFile("/home/mylog/test.txt", os.O_WRONLY|os.O_CREATE, 0666) defer file.Close() for { fmt.Fprintln(file, "hello world") } } 
  2. Build a docker image 建立一个Docker映像

docker build -t mylog .

Dockerfile Dockerfile

FROM golang
    RUN mkdir -p /home/mylog
    COPY main.go /go
    WORKDIR /go
    CMD ["go","run","main.go"]
  1. Run a container with -v option mouting the current dir. 运行带有-v选项的容器,将其装载到当前目录中。
docker run -d -v $PWD:/home/mylog mylog
  1. Check memory usage 检查内存使用情况
docker stats
  1. It's using 527MiB. 使用的是527MiB。
CONTAINER CPU% MEMUSAGE / LIMIT MEM% NET I/O BLOCK I/O PIDS 
     100.41% 527MiB / 15.5GiB 3.32% 648B /0B 72.3MB / 0B 15
  1. After a few seconds, it is 844.8 MiB 几秒钟后,就是844.8 MiB
CONTAINER CPU% MEMUSAGE / LIMIT MEM% NET I/O BLOCK I/O PIDS
     100.15% 844.8MiB / 15.5GiB 5.32% 648B /0B 72.3MB / 0B 15
  1. It keeps increasing and the host downs in the end. 它不断增加,主机最终崩溃。

Call this from time to time. 时不时拨打此电话。

file.Sync() 

https://golang.org/pkg/os/#File.Sync https://golang.org/pkg/os/#File.Sync

If you do not call this it writes to memory and waits for file.Close() in order to commit the changes to the file. 如果不调用它,它将写入内存并等待file.Close()以便将更改提交到文件。 And in this case Close in not called because it is in a defer (this means it is called when the function returns, and here it will never return since it is a never-ending for). 在这种情况下, Close in not called因为它处于defer (这意味着函数返回时会调用它,并且在这里它永远不会返回,因为它永远都不会结束)。

LE: Also try using: LE:也尝试使用:

file.WriteString("hello world")

instead of 代替

fmt.Fprintln(file, "hello world")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 docker - 装载的文件在容器中为空 - docker - mounted file is empty in container 如何解决 docker 容器增加 RSS memory - How to solve docker container increasing RSS memory 仅在安装SAN时允许写入目录 - Permit writing to directory only when SAN is mounted Docker 容器挂载文件共享路径未更新 - Docker container mounted file share path not updating 在 Docker 中作为目录安装的单个文件卷 - Single file volume mounted as directory in Docker 在主机上更改已装入卷中的文件时,未在docker容器中触发文件系统事件 - File system events not triggered in docker container when files in mounted volume are changed on the host docker 不断重启,直到在挂载的卷中创建文件 - docker keep restarting until file is created in a mounted volume 在Docker容器中运行命令时出现奇怪的“无此类文件或目录”错误 - Bizarre “No such file or directory” error when running a command within a docker container ls 命令结果在 CIFS 挂载目录中的 docker 容器中不正确(通过 docker-compose) - ls command result NOT CORRECT in docker container in a CIFS mounted directory (via docker-compose) Newrelic不显示Docker容器的CPU和内存使用情况 - Newrelic does not display CPU & memory usage for Docker's container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM