繁体   English   中英

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

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

我面临一个问题,当容器内的应用程序将日志写入已安装目录中的文件时,容器使用的内存会不断增加。

我希望内存使用量不会因此增加。 有谁知道为什么它增加? 谢谢 !!

这是我所做的:

  1. 编写一个仅将“ 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. 建立一个Docker映像

docker build -t mylog .

Dockerfile

FROM golang
    RUN mkdir -p /home/mylog
    COPY main.go /go
    WORKDIR /go
    CMD ["go","run","main.go"]
  1. 运行带有-v选项的容器,将其装载到当前目录中。
docker run -d -v $PWD:/home/mylog mylog
  1. 检查内存使用情况
docker stats
  1. 使用的是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. 几秒钟后,就是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. 它不断增加,主机最终崩溃。

时不时拨打此电话。

file.Sync() 

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

如果不调用它,它将写入内存并等待file.Close()以便将更改提交到文件。 在这种情况下, Close in not called因为它处于defer (这意味着函数返回时会调用它,并且在这里它永远不会返回,因为它永远都不会结束)。

LE:也尝试使用:

file.WriteString("hello world")

代替

fmt.Fprintln(file, "hello world")

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM