Golang runtime.ReadMemStats() function and MemStats type example

package runtime

Golang runtime.ReadMemStats() function and MemStats type usage example.

NOTE : Very useful in cases like where you want to have accurate breakdown of how Go sees the memory. For example, take snapshot of MemStats to build a memory analyzer application.

 package main

 import (
 "fmt"
 "runtime"
 )

 func main() {
 s := new(runtime.MemStats)
 runtime.ReadMemStats(s)

 fmt.Println("Alloc : ", s.Alloc)
 fmt.Println("Total Alloc : ", s.TotalAlloc)
 fmt.Println("Sys : ", s.Sys)
 fmt.Println("Lookups : ", s.Lookups)
 }

References :

http://golang.org/pkg/runtime/#ReadMemStats

http://golang.org/pkg/runtime/#MemStats

Advertisement