Skip to content

Instantly share code, notes, and snippets.

@RaheelYawar
Created July 31, 2018 16:25
Show Gist options
  • Save RaheelYawar/431ea1751da4caa1ff9171467cbd9d03 to your computer and use it in GitHub Desktop.
Save RaheelYawar/431ea1751da4caa1ff9171467cbd9d03 to your computer and use it in GitHub Desktop.
Golang Memory Profiler
var memProfile = flag.String("memprofile", "./profile/mem.prof", "write profile to file")
// Run the memory profiler and write profile to file.
func executeMemoryProfiler() {
flag.Parse()
if *memProfile != "" {
f, err := os.Create(*memProfile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment