Why my Go application doesn't free the memory

I’ve managed to find out the root cause - I run the pprof with default type --inuse_space but the thing I needed is --alloc_space:

go tool pprof --alloc_space http://host/debug/pprof/heap

For now, I see more clear picture of app behavior, but again it makes me confused:

The most “hungry” part is bytes.Buffer inside the cmd.Start().

I’ve tested cmd.CombinedOutput() from examples:

func main() {
	cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr")
	stdoutStderr, err := cmd.CombinedOutput()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", stdoutStderr)
}

Building it with additional flags, I can understand that the output will always be allocated on the heap:

$ go build -gcflags '-m -l'
./main.go:10:21: ... argument does not escape
./main.go:13:12: ... argument does not escape
./main.go:15:12: ... argument does not escape
./main.go:15:13: stdoutStderr escapes to heap

I have 2 questions: is there a way to avoid heap allocation when running exec and you need both: stdout and stderr, and why the output was not GCed?

Thank you,
P.

1 Like