Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt.Println cause memory escape #31317

Closed
qdongxu opened this issue Apr 7, 2019 · 4 comments
Closed

fmt.Println cause memory escape #31317

qdongxu opened this issue Apr 7, 2019 · 4 comments

Comments

@qdongxu
Copy link

qdongxu commented Apr 7, 2019

What version of Go are you using (go version)?

$ go version
go version go1.11.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/hg/4yk5n5f551z8v950nxkq9bf80000gn/T/go-build076297954=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
	"time"
)

func main() {
	for i := 0; i < 5; i++ {
        m := i
        fmt.Println("outer: ", m)
        go func(j int) {
            k := j
            fmt.Println("innter", k)
        }(i)
	}

	time.Sleep(1 * time.Second)
    s := "Done"
	fmt.Println(s)
}
% go build --gcflags '-m -l'  x.go
# command-line-arguments
./x.go:11:21: "outer: " escapes to heap
./x.go:11:21: m escapes to heap
./x.go:12:12: func literal escapes to heap
./x.go:12:12: func literal escapes to heap
./x.go:20:13: s escapes to heap
./x.go:14:25: "innter" escapes to heap
./x.go:14:25: k escapes to heap
./x.go:11:20: main ... argument does not escape
./x.go:20:13: main ... argument does not escape
./x.go:14:24: main.func1 ... argument does not escape

As we can see, the const strings, the variable j are allocated on heap, but they can be alloced on stack, by a easy manually check.

What did you expect to see?

the golang library funciton fmt.Println should not cause memory escape.

What did you see instead?

parameters passed into fmt.Println escaped to heap.

@qdongxu
Copy link
Author

qdongxu commented Apr 7, 2019

Looks there's already reported by #7710

@randall77
Copy link
Contributor

I think the right dup is #19720

TL;DR It would be nice to fix, but this is hard. It requires type-dependent escape analysis.

@zigo101
Copy link

zigo101 commented Apr 8, 2019

The still opening one #8618

@bcmills
Copy link
Contributor

bcmills commented Apr 10, 2019

Duplicate of #8618

@bcmills bcmills closed this as completed Apr 10, 2019
@bcmills bcmills marked this as a duplicate of #8618 Apr 10, 2019
@golang golang locked and limited conversation to collaborators Apr 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants