Standard library / runtime/pprof

pprof.WriteHeapProfile()

Write a heap profile in pprof format. A preceding GC can reduce ambiguity about live heap objects. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func WriteHeapProfile(w io.Writer) error

Parameters & Attributes

NameTypeDescription
wio.WriterDestination writer; keep it open until profiling or tracing has stopped.

Return Value

error

Error if writing the heap profile fails, otherwise nil.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "runtime"  "runtime/pprof")
func main() {  runtime.GC()  var b bytes.Buffer  if err := pprof.WriteHeapProfile(&b); err != nil {    panic(err)  }  fmt.Println(b.Len() > 2 && b.Bytes()[0] == 0x1f && b.Bytes()[1] == 0x8b)}
Go Reference