
回复
知道golang的内存逃逸吗?什么情况下会发生内存逃逸?
golang程序变量
会携带有一组校验数据,用来证明它的整个生命周期是否在运行时完全可知。如果变量通过了这些校验,它就可以在栈上
分配。否则就说它 逃逸
了,必须在堆上分配
。
能引起变量逃逸到堆上的典型情况:
go build -gcflags=-m
查看逃逸的情况。
package main
import "fmt"
type A struct {
s string
}
// 这是上面提到的 "在方法内把局部变量指针返回" 的情况
func foo(s string) *A {
a := new(A)
a.s = s
return a //返回局部变量a,在C语言中妥妥野指针,但在go则ok,但a会逃逸到堆
}
func main() {
a := foo("hello")
b := a.s + " world"
c := b + "!"
fmt.Println(c)
}
执行go build -gcflags=-m main.go
go build -gcflags=-m main.go
# command-line-arguments
./main.go:7:6: can inline foo
./main.go:13:10: inlining call to foo
./main.go:16:13: inlining call to fmt.Println
/var/folders/45/qx9lfw2s2zzgvhzg3mtzkwzc0000gn/T/go-build409982591/b001/_gomod_.go:6:6: can inline init.0
./main.go:7:10: leaking param: s
./main.go:8:10: new(A) escapes to heap
./main.go:16:13: io.Writer(os.Stdout) escapes to heap
./main.go:16:13: c escapes to heap
./main.go:15:9: b + "!" escapes to heap
./main.go:13:10: main new(A) does not escape
./main.go:14:11: main a.s + " world" does not escape
./main.go:16:13: main []interface {} literal does not escape
<autogenerated>:1: os.(*File).close .this does not escape
./main.go:8:10: new(A) escapes to heap
说明new(A)
逃逸了,符合上述提到的常见情况中的第一种。./main.go:14:11: main a.s + " world" does not escape
说明b
变量没有逃逸,因为它只在方法内存在,会在方法结束时被回收。./main.go:15:9: b + "!" escapes to heap
说明c
变量逃逸,通过fmt.Println(a ...interface{})
打印的变量,都会发生逃逸,感兴趣的朋友可以去查查为什么。文章转载自公众号:小白debug