Skip to content

Commit

Permalink
runtime/pprof: read memstats earlier in profile handler
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rsc committed Dec 1, 2017
1 parent 9cc9f10 commit 301b127
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/runtime/pprof/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 301b127

Please sign in to comment.