[英]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: 这是我所做的:
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") } }
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"]
docker run -d -v $PWD:/home/mylog mylog
docker stats
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
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
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.