Created
May 18, 2024 11:17
-
-
Save obutora/b6b85911a0e8ca5f7177bd2f2b3c3eda to your computer and use it in GitHub Desktop.
goでメモリ使用量を調べる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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