runtime.MemStats ã使ãã¨ç¾æç¹ã§ã®ã¡ã¢ãªã®ä½¿ç¨ç¶æ³ãªã©ã確èªã§ãã¾ãã
ã¨ããããã以ä¸ã®ãããªã³ã¼ããåããã¨Alloc (å²ãå½ã¦ããããã¼ããªãã¸ã§ã¯ãã®ãã¤ãæ°) ã¨NumGC (GCã®åæ°) ã確èªã§ãã¾ããã¤ãã§ã«æéä¸ã®æ大ã®Allocã確èªãã¦ãã¾ãã
ãã®ä»å¿ è¦ãªãã¼ã¿ã¯MemStatsã®ããã¥ã¡ã³ããèªã¿ãªããé©å®è¿½å ãããã¨ã
package main import ( "log" "runtime" "time" ) func main() { tick := time.NewTicker(2 * time.Second) defer tick.Stop() go func() { var maxAlloc uint64 for range tick.C { var s runtime.MemStats runtime.ReadMemStats(&s) toMB := func(v uint64) uint64 { return v / (1024 * 1024) } alloc := s.Alloc if maxAlloc < alloc { maxAlloc = alloc } log.Println("===============================") log.Printf("Alloc: %d\n", toMB(alloc)) log.Printf("MaxAlloc: %d\n", toMB(maxAlloc)) log.Printf("NumGC: %d\n", s.NumGC) log.Println("===============================") } }() someMemoryBoundFunction() }