知道golang的内存逃逸吗?什么状况下会发生内存逃逸?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
markdown
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)
逃逸了,符合上述提到的常见状况中的第一种。app
./main.go:14:11: main a.s + " world" does not escape
说明 b
变量没有逃逸,由于它只在方法内存在,会在方法结束时被回收。oop
./main.go:15:9: b + "!" escapes to heap
说明 c
变量逃逸,经过fmt.Println(a ...interface{})
打印的变量,都会发生逃逸,感兴趣的朋友能够去查查为何。学习
以上操做其实就叫逃逸分析。下篇文章,跟你们聊聊怎么用一个比较trick的方法使变量不逃逸。方便你们在面试官面前秀一波。ui
中文、数字、英文字母
的字符串