Skip to content

Instantly share code, notes, and snippets.

@seanhagen
Created October 24, 2018 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanhagen/b21cfe15f8486735756c0e21c1b37e3e to your computer and use it in GitHub Desktop.
Save seanhagen/b21cfe15f8486735756c0e21c1b37e3e to your computer and use it in GitHub Desktop.
Golang func to print memory usage
func PrintMemUsage(id int, name string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf(
"Call %v from %v -- Alloc = %v MiB\tTotalAlloc = %v MiB\tSys = %v MiB\tNumGC = %v\n",
id,
name,
bToMb(m.Alloc),
bToMb(m.TotalAlloc),
bToMb(m.Sys),
m.NumGC,
)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment