Go: Escape analysis analogy seems not correct

Created on 4 Mar 2019  路  6Comments  路  Source: golang/go

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

go version go1.11.4 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:Usersrama.moorthyAppDataLocalgo-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:Usersrama.moorthy
set GOPROXY=
set GORACE=
set GOROOT=C:Go
set GOTMPDIR=
set GOTOOLDIR=C:Gopkgtoolwindows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:UsersRAMA~1.MOOAppDataLocalTempgo-build138480882=/tmp/go-build -gno-record-gcc-switches

What did you do?

escape analysis says that slice array b is moved to heap , but after printing the address of slice array , its address is adjacent to stack variable i address.

This is the program

image

https://play.golang.org/p/wGjw7wPQgu9
-->

What did you expect to see?

i expect to see the address of b should not be adjacent to stack address

What did you see instead?

image

FrozenDueToAge

Most helpful comment

The address of b is a stack address. The address of the first element of the slice is &b[0], not &b.
You might want to read https://blog.golang.org/go-slices-usage-and-internals

All 6 comments

The address of b is a stack address. The address of the first element of the slice is &b[0], not &b.
You might want to read https://blog.golang.org/go-slices-usage-and-internals

Thanks @randall77 .

I read the slice internal doc. but still why slice is allocated in stack rather than in heap. escape analysis says it will allocate into heap.

The backing store for the slice (the array) is allocated on the heap. The slice itself (the 3-word structure consisting of pointer to the array, a length, and a capacity) is allocated on the stack.

When the escape analysis says "b escapes to heap", it means that the values in b are written to the heap. So anything referenced by b must be in the heap also. b itself need not be.

If b itself needs to be on the heap, escape analysis will say "&b escapes to heap".

Hi @randall77 , Yes you are right, address of b[0] should refer to heap address(lower address in virtual memory). but its not pointing

address of i --> 0xc00007ff50

address of b[0], and b[1] --> 0xc00005a070, 0xc00005a071

my objective is that i wanna make sure that backing array is pointing to heap memory according to escape analysis.

Thanks.

Your assumption that heap addresses are lower in virtual memory is incorrect. Pages of Go heap and pages of goroutine stacks are intermingled.

@randall77 , Thanks... that clears all my questions.

Was this page helpful?
0 / 5 - 0 ratings