[英]docker container keep increasing memory usage when writing into a file in a mounted directory
我面臨一個問題,當容器內的應用程序將日志寫入已安裝目錄中的文件時,容器使用的內存會不斷增加。
我希望內存使用量不會因此增加。 有誰知道為什么它增加? 謝謝 !!
這是我所做的:
編寫一個僅將“ 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") } }
建立一個Docker映像
docker build -t mylog .
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
時不時撥打此電話。
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.