Skip to content

Instantly share code, notes, and snippets.

@obutora
Created May 18, 2024 11:17
Show Gist options
  • Save obutora/b6b85911a0e8ca5f7177bd2f2b3c3eda to your computer and use it in GitHub Desktop.
Save obutora/b6b85911a0e8ca5f7177bd2f2b3c3eda to your computer and use it in GitHub Desktop.
goでメモリ使用量を調べる
func main() {
mem := func () uint64 {
runtime.GC()
var s runtime.MemStats
runtime.ReadMemStats(&s)
// Sys 以外にも [Alloc, TotalAlloc, Mallocs, Frees, HeapAlloc, HeapSys, HeapIdle, HeapInuse, HeapReleased, HeapObjects, StackInuse, StackSys, MSpanInuse, MSpanSys, MCacheInuse, MCacheSys, BuckHashSys, GCSys, OtherSys] などがある
return s.Sys
}
before := mem()
// ここにメモリを使う処理を書く
after := mem()
fmt.Printf("used memory: %.3fkb\n", float64(after - before))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment