Download the test code

You can obtain the test program from go Get. Add -d to avoid automatic installation after downloading.

Github

go get -d github.com/wolfogre/go-pprof-practice
cd $GOPATH/src/github.com/wolfogre/go-pprof-practice
Copy the code

If you can’t download go Get, you can download git Clone

git clone https://github.com/wolfogre/go-pprof-practice
Copy the code

Compile the code

Then run

go mod init
go mod tidy
Copy the code

Last run

go build
./go-pprof-practice
Copy the code

Run the pprof command

go tool pprof http://localhost:6060/debug/pprof/heap
Copy the code

Or three axetop.listSuch as the command

listThe command

Can see the problem in github.com/wolfogre/go-pprof-practice/animal/muridae/mouse. Mouse (*). Steal, function content is as follows:

func (m *Mouse) Steal() {
	log.Println(m.Name(), "steal")
	max := constant.Gi
	for len(m.buffer) * constant.Mi < max {
		m.buffer = append(m.buffer, [constant.Mi]byte{})
	}
}
Copy the code

As you can see, there is a loop that appends an array of 1 MiB length to the m.buffer until the total capacity reaches 1 GiB, and does not free the memory, which explains the high memory footprint. usewebTo view the graphical display, you can double check that the problem is really here:

Welcome to pay attention to the public number: programmer wealth free road

The resources
  • Blog.wolfogre.com/posts/go-pp…