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".
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 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
Call this from time to time.
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. 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).
LE: Also try using:
file.WriteString("hello world")
instead of
fmt.Fprintln(file, "hello world")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.