douqu2481 2017-12-18 09:39
浏览 973
已采纳

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

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".

    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 build -t mylog .

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.

    docker run -d -v $PWD:/home/mylog mylog 

  1. Check memory usage

    docker stats 

  1. It's using 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

    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.
  • 写回答

1条回答 默认 最新

  • dstm2014 2017-12-18 10:17
    关注

    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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)