package main import ( "fmt" "os" "runtime" "runtime/debug" "runtime/trace" "time" ) func printGCStats() { t := time.NewTicker(time.Second) s := debug.GCStats{} for { select { case <-t.C: debug.ReadGCStats(&s) fmt.Printf("gc %d last@%v, PauseTotal %v\n", s.NumGC, s.LastGC, s.PauseTotal) } } } func printMemStats() { t := time.NewTicker(time.Second) s := runtime.MemStats{} for { select { case <-t.C: runtime.ReadMemStats(&s) fmt.Printf("gc %d last@%v, next_heap_size@%vMB\n", s.NumGC, time.Unix(int64(time.Duration(s.LastGC).Seconds()), 0), s.NextGC/(1<<20)) } } } func allocate() { _ = make([]byte, 1<<20) } func main() { // go printGCStats() // go printMemStats() f, _ := os.Create("trace.out") defer f.Close() trace.Start(f) defer trace.Stop() for n := 1; n < 100000; n++ { allocate() } }