github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/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 // Force heap sampling for determinism. 25 runtime.MemProfileRate = 1 26 27 for i := 0; i < 10; i++ { 28 fmt.Fprintf(&memProfBuf, "%*d\n", i, i) 29 } 30 memProfStr = memProfBuf.String() 31 32 runtime.GC() 33 34 f, err := ioutil.TempFile("", "memprof") 35 if err != nil { 36 fmt.Fprintln(os.Stderr, err) 37 os.Exit(2) 38 } 39 40 if err := pprof.WriteHeapProfile(f); err != nil { 41 fmt.Fprintln(os.Stderr, err) 42 os.Exit(2) 43 } 44 45 name := f.Name() 46 if err := f.Close(); err != nil { 47 fmt.Fprintln(os.Stderr, err) 48 os.Exit(2) 49 } 50 51 fmt.Println(name) 52 }