1
0
mirror of https://github.com/golang/go synced 2024-11-19 12:34:47 -07:00

runtime/pprof: read memstats earlier in profile handler

Reading the mem stats before our own allocations
avoids cluttering memory stats with our recent garbage.

Fixes #20565.

Change-Id: I3b0046c8300dca83cea24013ffebc32b2ae7f742
Reviewed-on: https://go-review.googlesource.com/80739
Reviewed-by: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Russ Cox 2017-11-29 15:38:52 -05:00
parent 9cc9f10855
commit 301b127a05

View File

@ -509,6 +509,14 @@ func countHeap() int {
// writeHeap writes the current runtime heap profile to w.
func writeHeap(w io.Writer, debug int) error {
var memStats *runtime.MemStats
if debug != 0 {
// Read mem stats first, so that our other allocations
// do not appear in the statistics.
memStats = new(runtime.MemStats)
runtime.ReadMemStats(memStats)
}
// Find out how many records there are (MemProfile(nil, true)),
// allocate that many records, and get the data.
// There's a race—more records might be added between
@ -571,8 +579,7 @@ func writeHeap(w io.Writer, debug int) error {
// Print memstats information too.
// Pprof will ignore, but useful for people
s := new(runtime.MemStats)
runtime.ReadMemStats(s)
s := memStats
fmt.Fprintf(w, "\n# runtime.MemStats\n")
fmt.Fprintf(w, "# Alloc = %d\n", s.Alloc)
fmt.Fprintf(w, "# TotalAlloc = %d\n", s.TotalAlloc)