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 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

This comment has been minimized.

Copy link
Author

@qdongxu qdongxu commented Apr 7, 2019

Looks there's already reported by #7710

@randall77

This comment has been minimized.

Copy link
Contributor

@randall77 randall77 commented Apr 7, 2019

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.

@go101

This comment has been minimized.

Copy link

@go101 go101 commented Apr 8, 2019

The still opening one #8618

@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Apr 10, 2019

Duplicate of #8618

@bcmills bcmills closed this Apr 10, 2019
@bcmills bcmills marked this as a duplicate of #8618 Apr 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.