当前位置:  开发笔记 > 编程语言 > 正文

“使用的内存”指标:工具pprof与docker统计信息

如何解决《“使用的内存”指标:工具pprof与docker统计信息》经验,为你挑选了1个好方法。

我编写了在每个Docker容器中运行的golang应用程序。它通过tcp和udp使用protobufs相互通信,我使用Hashicorp的memberlist库来发现网络中的每个容器。在docker stats上,我看到内存使用量呈线性增加,因此我试图查找应用程序中的任何泄漏。

由于它是一个可以继续运行的应用程序,因此请使用http pprof来检查任何一个容器中的实时应用程序。我看到即使docker stats线性增加,runtime.MemStats.sys也保持不变。我的--inuse_space约为1MB,当然--alloc_space随时间不断增加。这是alloc_space的示例:

root@n3:/app# go tool pprof --alloc_space main http://localhost:8080/debug/pprof/heap                                                                                                                       
Fetching profile from http://localhost:8080/debug/pprof/heap
Saved profile in /root/pprof/pprof.main.localhost:8080.alloc_objects.alloc_space.005.pb.gz
Entering interactive mode (type "help" for commands)
(pprof) top --cum
1024.11kB of 10298.19kB total ( 9.94%)
Dropped 8 nodes (cum <= 51.49kB)
Showing top 10 nodes out of 34 (cum >= 1536.07kB)
      flat  flat%   sum%        cum   cum%
         0     0%     0% 10298.19kB   100%  runtime.goexit
         0     0%     0%  6144.48kB 59.67%  main.Listener
         0     0%     0%  3072.20kB 29.83%  github.com/golang/protobuf/proto.Unmarshal
  512.10kB  4.97%  4.97%  3072.20kB 29.83%  github.com/golang/protobuf/proto.UnmarshalMerge
         0     0%  4.97%  2560.17kB 24.86%  github.com/hashicorp/memberlist.(*Memberlist).triggerFunc
         0     0%  4.97%  2560.10kB 24.86%  github.com/golang/protobuf/proto.(*Buffer).Unmarshal
         0     0%  4.97%  2560.10kB 24.86%  github.com/golang/protobuf/proto.(*Buffer).dec_struct_message
         0     0%  4.97%  2560.10kB 24.86%  github.com/golang/protobuf/proto.(*Buffer).unmarshalType
  512.01kB  4.97%  9.94%  2048.23kB 19.89%  main.SaveAsFile
         0     0%  9.94%  1536.07kB 14.92%  reflect.New
(pprof) list main.Listener
Total: 10.06MB
ROUTINE ======================== main.Listener in /app/listener.go
         0        6MB (flat, cum) 59.67% of Total
         .          .     24:   l.SetReadBuffer(MaxDatagramSize)
         .          .     25:   defer l.Close()
         .          .     26:   m := new(NewMsg)
         .          .     27:   b := make([]byte, MaxDatagramSize)
         .          .     28:   for {
         .   512.02kB     29:       n, src, err := l.ReadFromUDP(b)
         .          .     30:       if err != nil {
         .          .     31:           log.Fatal("ReadFromUDP failed:", err)
         .          .     32:       }
         .   512.02kB     33:       log.Println(n, "bytes read from", src)
         .          .     34:       //TODO remove later. For testing Fetcher only
         .          .     35:       if rand.Intn(100) < MCastDropPercent {
         .          .     36:           continue
         .          .     37:       }
         .        3MB     38:       err = proto.Unmarshal(b[:n], m)
         .          .     39:       if err != nil {
         .          .     40:           log.Fatal("protobuf Unmarshal failed", err)
         .          .     41:       }
         .          .     42:       id := m.GetHead().GetMsgId()
         .          .     43:       log.Println("CONFIG-UPDATE-RECEIVED { \"update_id\" =", id, "}")
         .          .     44:       //TODO check whether value already exists in store?
         .          .     45:       store.Add(id)
         .        2MB     46:       SaveAsFile(id, b[:n], StoreDir)
         .          .     47:       m.Reset()
         .          .     48:   }
         .          .     49:}
(pprof) 

我已经能够使用http://:8080 / debug / pprof / goroutine?debug = 1验证是否没有goroutine泄漏发生

请评论为什么docker stats显示不同的图片(线性增加内存)

CONTAINER           CPU %               MEM USAGE / LIMIT       MEM %               NET I/O               BLOCK I/O           PIDS
n3                  0.13%               19.73 MiB / 31.36 GiB   0.06%               595 kB / 806 B        0 B / 73.73 kB      14

如果我将其运行一整夜,则此内存会膨胀到250MB左右。我的运行时间没有超过此时间,但我认为这应该已经达到平稳状态,而不是线性增加



1> Anu..:

docker stats显示cgroup的内存使用情况统计信息。(请参阅:https : //docs.docker.com/engine/admin/runmetrics/)

如果您阅读“过时但有用的”文档(https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt),则会显示

5.5用法_in_bytes

为了提高效率,与其他内核组件一样,内存cgroup使用一些优化来避免不必要的缓存行错误共享。usage_in_bytes受此方法的影响,并且未显示内存(和交换)使用情况的“确切”值,它是有效访问的模糊值。(当然,在必要时,它是同步的。)如果要了解更精确的内存使用情况,则应在memory.stat(请参阅5.2)中使用RSS + CACHE(+ SWAP)值。

页面缓存和RES包含在内存usage_in_bytes编号中。因此,如果容器具有文件I / O,则内存使用情况统计将增加。但是,对于一个容器,如果使用量达到该最大限制,它将回收一些未使用的内存。因此,当我向容器添加内存限制时,可以观察到达到该限制时便回收并使用了内存。除非没有要回收的内存并且发生OOM错误,否则不会终止容器进程。对于任何关心docker stats中显示的数字的人,简单的方法是在以下位置检查cgroups中可用的详细统计信息:/ sys / fs / cgroup / memory / docker //这将详细显示内存中的所有内存指标。统计信息或其他memory。*文件。

如果要在“ docker run”命令中限制docker容器使用的资源,可以通过参考以下内容进行限制:https : //docs.docker.com/engine/admin/resource_constraints/

由于我使用的是docker-compose,因此我在docker-compose.yml文件中要限制的服务下添加了一行:

mem_limit:32m

其中m代表兆字节。

推荐阅读
爱唱歌的郭少文_
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有