go-profiler-notes/docs/code/memory-profiler-gc.go
Felix Geisendörfer 6342fc3259 updates
2022-12-29 14:05:20 +01:00

35 lines
559 B
Go

//go:build ignore
package main
import (
"log"
"os"
"runtime"
"runtime/pprof"
)
var buf [][]byte
var ballast []byte
func main() {
file, _ := os.Create("mem.pprof")
space := 5 * 1024 * 1024 // 5 MiB
allocs := 1000
ballast = make([]byte, space*5)
runtime.GC()
log.Println("start alloc")
for i := 0; i < allocs; i++ {
buf = append(buf, allocBytes(space/allocs))
}
runtime.GC() // without this the allocs above are invisible
log.Println("end alloc")
pprof.WriteHeapProfile(file)
}
func allocBytes(n int) []byte {
return make([]byte, n)
}