示例代码 test.go
package main
import (
"fmt"
"time"
)
func main() {
a := "123"
fmt.Println(a)
B := make(chan int, 8)
go func() {
B <- 1
}()
go func() {
fmt.Println(<-B)
}()
c := make([]int, 10, 20)
c = append(c, 1, 2, 3)
fmt.Println(c)
time.Sleep(1 * time.Second)
}
命令行调用go程序代码,输出gc信息到日志
GODEBUG=gctrace=1 go run test.go 2> log.file
123
[0 0 0 0 0 0 0 0 0 0 1 2 3]
1
gc 1 @0.030s 0%: 0.036+0.47+0.11 ms clock, 0.14+0.16/0.33/0.91+0.44 ms cpu, 4->4->0 MB, 5 MB goal, 4 P
gc 2 @0.052s 0%: 0.005+0.43+0.077 ms clock, 0.023+0.19/0.30/0.71+0.31 ms cpu, 4->4->0 MB, 5 MB goal, 4 P
gc 3 @0.082s 3%: 1.6+1.2+0.25 ms clock, 6.5+1.5/2.4/0+1.0 ms cpu, 4->4->1 MB, 5 MB goal, 4 P
gc 4 @0.089s 4%: 0.10+0.79+0.087 ms clock, 0.41+0.27/0.73/0.60+0.35 ms cpu, 4->4->1 MB, 5 MB goal, 4 P
gc 5 @0.096s 4%: 0.005+0.50+0.035 ms clock, 0.023+0.055/0.38/0.94+0.14 ms cpu, 4->4->0 MB, 5 MB goal, 4 P
gc 6 @0.102s 4%: 0.005+0.51+0.049 ms clock, 0.022+0.10/0.40/0.93+0.19 ms cpu, 4->4->1 MB, 5 MB goal, 4 P
# command-line-arguments
gc 1 @0.005s 9%: 0.008+3.3+0.071 ms clock, 0.035+0.18/3.0/2.4+0.28 ms cpu, 4->5->3 MB, 5 MB goal, 4 P
# command-line-arguments
gc 1 @0.001s 18%: 0.006+3.7+0.087 ms clock, 0.026+0.16/3.5/0.30+0.35 ms cpu, 4->5->4 MB, 5 MB goal, 4 P
gc 2 @0.012s 11%: 0.006+3.5+0.034 ms clock, 0.026+0.10/3.3/0.94+0.13 ms cpu, 8->9->8 MB, 9 MB goal, 4 P
gc 3 @0.020s 13%: 0.006+6.1+0.056 ms clock, 0.024+0.16/6.0/3.4+0.22 ms cpu, 14->14->14 MB, 16 MB goal, 4 P
gc 4 @0.060s 8%: 0.009+11+0.041 ms clock, 0.038+0.19/11/6.6+0.16 ms cpu, 26->26->24 MB, 28 MB goal, 4 P
golang gc 优化思路以及实例分析
有疑问加站长微信联系(非本文作者)