55 "io"
66 "os"
77 "runtime"
8+ "runtime/debug"
9+ "runtime/metrics"
810 "time"
911)
1012
@@ -28,10 +30,29 @@ func displayStatsWithWriter(writer io.Writer, interval ...time.Duration) {
2830 ticker := time .NewTicker (duration )
2931 defer ticker .Stop ()
3032 for range ticker .C {
31- var m runtime.MemStats
32- runtime .ReadMemStats (& m )
33+ var (
34+ alloc , totalAlloc , sys uint64
35+ samples = []metrics.Sample {
36+ {Name : "/memory/classes/heap/objects:bytes" },
37+ {Name : "/gc/heap/allocs:bytes" },
38+ {Name : "/memory/classes/total:bytes" },
39+ }
40+ )
41+ metrics .Read (samples )
42+
43+ if samples [0 ].Value .Kind () == metrics .KindUint64 {
44+ alloc = samples [0 ].Value .Uint64 ()
45+ }
46+ if samples [1 ].Value .Kind () == metrics .KindUint64 {
47+ totalAlloc = samples [1 ].Value .Uint64 ()
48+ }
49+ if samples [2 ].Value .Kind () == metrics .KindUint64 {
50+ sys = samples [2 ].Value .Uint64 ()
51+ }
52+ var stats debug.GCStats
53+ debug .ReadGCStats (& stats )
3354 fmt .Fprintf (writer , "Goroutines: %d, Alloc: %vm, TotalAlloc: %vm, Sys: %vm, NumGC: %v\n " ,
34- runtime .NumGoroutine (), m . Alloc / mega , m . TotalAlloc / mega , m . Sys / mega , m .NumGC )
55+ runtime .NumGoroutine (), alloc / mega , totalAlloc / mega , sys / mega , stats .NumGC )
3556 }
3657 }()
3758}
0 commit comments