github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/runtime/testdata/testprog/memprof.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"runtime"
    13  	"runtime/pprof"
    14  )
    15  
    16  func init() {
    17  	register("MemProf", MemProf)
    18  }
    19  
    20  var memProfBuf bytes.Buffer
    21  var memProfStr string
    22  
    23  func MemProf() {
    24  	for i := 0; i < 1000; i++ {
    25  		fmt.Fprintf(&memProfBuf, "%*d\n", i, i)
    26  	}
    27  	memProfStr = memProfBuf.String()
    28  
    29  	runtime.GC()
    30  
    31  	f, err := ioutil.TempFile("", "memprof")
    32  	if err != nil {
    33  		fmt.Fprintln(os.Stderr, err)
    34  		os.Exit(2)
    35  	}
    36  
    37  	if err := pprof.WriteHeapProfile(f); err != nil {
    38  		fmt.Fprintln(os.Stderr, err)
    39  		os.Exit(2)
    40  	}
    41  
    42  	name := f.Name()
    43  	if err := f.Close(); err != nil {
    44  		fmt.Fprintln(os.Stderr, err)
    45  		os.Exit(2)
    46  	}
    47  
    48  	fmt.Println(name)
    49  }