Why does function escape to heap?

94 views
Skip to first unread message

Pure White

unread,
Jun 29, 2020, 11:15:17 AM6/29/20
to golang-nuts
Hi,
    I've got a piece of code like this https://play.golang.org/p/B8bVCcJIg3B, and the escape analyzer indicates that m.myFunc escapes to heap:
$ go run -gcflags="-m" main.go
# command-line-arguments
./main.go:39:13: inlining call to fmt.Println
./main.go:14:6: can inline (*myFuncImplStruct).myFunc-fm
./main.go:28:23: leaking param: f
./main.go:29:7: &myFuncContainer literal escapes to heap
./main.go:35:7: &myFuncImplStruct literal escapes to heap
./main.go:37:26: m.myFunc escapes to heap
./main.go:39:13: []interface {} literal does not escape
<autogenerated>:1: .this does not escape
    The asm code shows that there's a call to runtime.newobject which I think allocs a new object for m.myFunc:
    I would like to know:
    1. How can a function "escapes to heap"? Isn't it a pointer to the code area?
    2. Why does the receiver function "escapes to heap" while the normal function does not?
    3. What's the new object allocated?
    Thanks very much!

Jan Mercl

unread,
Jun 29, 2020, 11:49:21 AM6/29/20
to Pure White, golang-nuts
On Mon, Jun 29, 2020 at 5:15 PM Pure White <wu.pur...@gmail.com> wrote:

> I would like to know:
> 1. How can a function "escapes to heap"? Isn't it a pointer to the code area?

It is not: https://groups.google.com/forum/#!searchin/golang-dev/plan$20for$20two-word$20funcs$2C$20take$202%7Csort:date/golang-dev/x328N8hiN5k/i5LJIXGmi_gJ
Reply all
Reply to author
Forward
0 new messages