2

尝试检查go程序中的局部变量是否分配在堆或堆栈上,并且无法确定go的gc的某些输出的含义。


代码

variable_heap_stack.go:

// variable heap & stack learn,
// run with:
//  go run -gcflags -m xxx.go
package main

import "fmt"

func getPointerOfLocalVar() *int {
    x := 10 // go will put it into heap,
    return &x
}

// heap & stack test,
func heapStackTest() {
    px := getPointerOfLocalVar()
    fmt.Printf("x: %d\n", *px)

    y := 20 // go will put it into stack,
    fmt.Printf("y: %d\n", y)
}

func main() {
    heapStackTest()
}

执行:

运行 -gcflags -m variable_heap_stack.go

输出:

# command-line-arguments
./variable_heap_stack.go:8:6: can inline getPointerOfLocalVar
./variable_heap_stack.go:15:28: inlining call to getPointerOfLocalVar
./variable_heap_stack.go:10:9: &x escapes to heap
./variable_heap_stack.go:9:6: moved to heap: x
./variable_heap_stack.go:16:24: *px escapes to heap
./variable_heap_stack.go:19:13: y escapes to heap
./variable_heap_stack.go:15:28: heapStackTest &x does not escape
./variable_heap_stack.go:16:12: heapStackTest ... argument does not escape
./variable_heap_stack.go:19:12: heapStackTest ... argument does not escape
x: 10
y: 20

问题

  • 是什么escapes to heap意思?它会堆积还是不堆积?
  • moved to heap,这意味着移动到堆,对吧?和上面的有什么区别?
  • 变量是本地的y,函数返回后没有人引用它,但仍然有一行y escapes to heap,这是为什么?
4

1 回答 1

4

是什么escapes to heap意思?它会堆积还是不堆积?

这意味着消息中指示的值离开了函数的“边界”,因此,不能保证它在函数之外会发生什么,因此如果该值是指针或引用(但仅限于此),则必须在堆上分配指向或引用的值。

您可以将其escapes to heap视为调试消息,它并不表示您的变量之一被“重新定位”到堆中。

所以简单地说,“逃到堆”类似于术语:“它离开函数”,或者“它被传递到函数之外”

例如这一行:

./variable_heap_stack.go:16:24: *px escapes to heap

表示该值*px是在函数外部传递的,即作为fmt.Printf()此行中的参数:

fmt.Printf("x: %d\n", *px)

moved to heap,这意味着移动到堆,对吧?和上面的有什么区别?

这表明编译器决定将消息中指示的变量移动到堆中,因为它可能在函数外部被引用,因此它必须在函数中存在。并且由于从函数返回后堆栈分配的值可能会变得无效,因此要使指示的变量在函数返回后有效,它必须在堆上。

Moved to heap直接宣布您的变量之一确实“重新定位”到了堆中。注意:“重定位”意味着变量将首先分配到堆上,实际的“重定位”无论如何都不会发生。

变量是本地的y,函数返回后没有人引用它,但仍然有一行y escapes to heap,这是为什么?

如前所述,这并不意味着y被重定位到堆中,它只是意味着将值y传递到函数之外,即作为参数传递给fmt.Printf()这一行:

fmt.Printf("y: %d\n", y)

y不会因为这个而被移动到堆中,没有必要,因为它是通过fmt.Printf()复制其值来传递的,并且fmt.Printf()无法到达您的y局部变量。

提示:

-m您可以通过像这样传递两次来获取有关优化决策和逃逸分析的更多详细信息:

go run -gcflags='-m -m' variable_heap_stack.go

那么这个命令的输出将是:

./variable_heap_stack.go:8:6: can inline getPointerOfLocalVar as: func() *int { x := 10; return &x }
./variable_heap_stack.go:14:6: cannot inline heapStackTest: non-leaf function
./variable_heap_stack.go:15:28: inlining call to getPointerOfLocalVar func() *int { x := 10; return &x }
./variable_heap_stack.go:22:6: cannot inline main: non-leaf function
./variable_heap_stack.go:10:9: &x escapes to heap
./variable_heap_stack.go:10:9:         from ~r0 (return) at ./variable_heap_stack.go:10:2
./variable_heap_stack.go:9:2: moved to heap: x
./variable_heap_stack.go:16:24: *px escapes to heap
./variable_heap_stack.go:16:24:        from ... argument (arg to ...) at ./variable_heap_stack.go:16:12
./variable_heap_stack.go:16:24:        from *(... argument) (indirection) at ./variable_heap_stack.go:16:12
./variable_heap_stack.go:16:24:        from ... argument (passed to call[argument content escapes]) at ./variable_heap_stack.go:16:12
./variable_heap_stack.go:19:13: y escapes to heap
./variable_heap_stack.go:19:13:        from ... argument (arg to ...) at ./variable_heap_stack.go:19:12
./variable_heap_stack.go:19:13:        from *(... argument) (indirection) at ./variable_heap_stack.go:19:12
./variable_heap_stack.go:19:13:        from ... argument (passed to call[argument content escapes]) at ./variable_heap_stack.go:19:12
./variable_heap_stack.go:15:28: heapStackTest &x does not escape
./variable_heap_stack.go:16:12: heapStackTest ... argument does not escape
./variable_heap_stack.go:19:12: heapStackTest ... argument does not escape
x: 10
y: 20
于 2018-07-25T13:41:52.697 回答