Created
March 27, 2019 17:24
-
-
Save stackdump/372be5085154b5d0968613c1787e5142 to your computer and use it in GitHub Desktop.
This file contains 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 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