Skip to content

Instantly share code, notes, and snippets.

@stackdump
Created March 27, 2019 17:24
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 stackdump/372be5085154b5d0968613c1787e5142 to your computer and use it in GitHub Desktop.
Save stackdump/372be5085154b5d0968613c1787e5142 to your computer and use it in GitHub Desktop.
func MemProfiler() {
// record memory profile on a loop
ticker := time.NewTicker(time.Second)
for {
<-ticker.C
st := &runtime.MemStats{}
runtime.ReadMemStats(st)
// From Golang docs: HeapObjects increases as objects are allocated
// and decreases as the heap is swept and unreachable objects are
// freed.
/*
fmt.Println("Heap allocs:", st.Mallocs, "Heap frees:",
st.Frees, "Heap objects:", st.HeapObjects)
*/
// REVIEW: couldn't figure out how to collect multiple profiles in the same file
if f, err := os.Create(fmt.Sprintf("factomd.%v.memprof", time.Now().UnixNano())); err != nil {
fmt.Printf("record memory profile failed: %v", err)
} else {
runtime.GC() // trigger garbage collection first
pprof.WriteHeapProfile(f)
f.Close()
//fmt.Println("record memory profile")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment