Golang内存逃逸
南一道街丶 人气:01.什么是内存逃逸
在一段程序中,每一个函数都会有自己的内存区域分配自己的局部变量,返回值,这些内存会由编译器在栈中进行分配,每一个函数会分配一个栈帧,在函数运行结束后销毁,但是有些变量我们想在函数运行结束后仍然使用,就需要把这个变量分配在堆上,这种从“栈”上逃逸到“堆”上的现象叫做内存逃逸
2.什么是逃逸分析
虽然Go语言引入的Gc,GC机制会对堆上的对象进行管理,当某个对象不可达(没有其他对象引用他),他将会被回收。虽然GC可以降低工作人员负担,但是GC也会给程序带来性能损耗,当堆内存上有大量的堆内存对象,就会给GC很大的压力,虽然Go语言使用的是标记清除算法,并且在此基础上使用了三色标记法和写屏障技术,但是我们在堆上分配大量内存,仍然会对GC造成很大压力,Go引入了逃逸分析,就是想减少堆内存的分配,可以在栈分配的内存尽量分配在栈上
3.小结
逃逸分析就是在程序编译阶段根据代码中的数据流,对代码中哪些变量需要在栈上分配,哪些需要在对象分配的静态分析方法,堆和栈相比,堆适合分配不可预知大小的内存,但是付出代价是分配速度慢,容易产生碎片,栈分配十分快,栈分配只需要两个指令“Push”和"Release"分配和释放,而且堆分配需要先找一块适合大小的内存块分配,需要垃圾回收释放,所以逃逸分析可以更好的做内存分配
Go语言的逃逸分析
src/cmd/compile/internal/gc/escape.go
- pointers to stack objects cannot be stored in the heap: 指向栈对象的指针不能存储在堆中
- pointers to a stack object cannot outlive that object:指向栈对象的指针不能超过该对象的存活期,指针不能在栈对象销毁之后依然存活(例子:声明的函数返回并销毁了对象的栈帧,或者它在循环迭代中被重复用于逻辑上不同的变量)
既然逃逸分析是在编译阶段进行的,那我们就可以通过go build -gcflga '-m -m l'查看逃逸分析结果
4.逃逸分析案例
1.函数返回局部指针变量
func Add(x,y int) *int { res := 0 res = x + y return &res } func main() { Add(1,2) }
.\pointer.go:4:2: res escapes to heap:
.\pointer.go:4:2: flow: ~r2 = &res:
.\pointer.go:4:2: from &res (address-of) at .\pointer.go:6:9
.\pointer.go:4:2: from return &res (return) at .\pointer.go:6:2
.\pointer.go:4:2: moved to heap: res
函数返回局部变量是一个指针变量,函数Add执行结束,对应栈帧就会销毁,但是引用返回到函数外部,如果我们外部解析地址,就会导致程序访问非法内存,所以经过编辑器分析过后将其在堆上分配
2.interface类型逃逸
1.interface产生逃逸
func main() { str := "荔枝" fmt.Println(str) }
E:\GoStudy\src\HighBase\Escape>go build -gcflags="-m -m -l" ./pointer.go
# command-line-arguments
.\pointer.go:20:13: str escapes to heap:
.\pointer.go:20:13: flow: {storage for ... argument} = &{storage for str}:
.\pointer.go:20:13: from str (spill) at .\pointer.go:20:13
.\pointer.go:20:13: from ... argument (slice-literal-element) at .\pointer.go:20:13
.\pointer.go:20:13: flow: {heap} = {storage for ... argument}:
.\pointer.go:20:13: from ... argument (spill) at .\pointer.go:20:13
.\pointer.go:20:13: from fmt.Println(... argument...) (call parameter) at .\pointer.go:20:13
.\pointer.go:20:13: ... argument does not escape
.\pointer.go:20:13: str escapes to heap
str是main的一个局部变量,传给 fmt.Printl()之后逃逸,因为fmt.Println()的入参是interface{}类型,如果参数为interface{},那么编译期间就很难确定参数类型
2.指向栈对象的指针不能在堆中
我们把代码改成这样
func main() { str := "苏珊" fmt.Println(&str) }
# command-line-arguments
.\pointer.go:19:2: str escapes to heap:
.\pointer.go:19:2: flow: {storage for ... argument} = &str:
.\pointer.go:19:2: from &str (address-of) at .\pointer.go:20:14
.\pointer.go:19:2: from &str (interface-converted) at .\pointer.go:20:14
.\pointer.go:19:2: from ... argument (slice-literal-element) at .\pointer.go:20:13
.\pointer.go:19:2: flow: {heap} = {storage for ... argument}:
.\pointer.go:19:2: from ... argument (spill) at .\pointer.go:20:13
.\pointer.go:19:2: from fmt.Println(... argument...) (call parameter) at .\pointer.go:20:13
.\pointer.go:19:2: moved to heap: str
.\pointer.go:20:13: ... argument does not escape
这次str也逃逸到堆上面了,在堆上面进行分配,因为入参是interface,变量str的地址被以实参的方式传入fmt.Println被装箱到一个interface{}
装箱的形参变量要在堆上分配,但是还需要存储一个栈上的地址,这和之前说的第一条不符,所以str也会分配到堆上
3.闭包产生逃逸
func Increase() func() int { n := 0 return func() int { n++ return n } } func main() { in := Increase() fmt.Println(in()) // 1 }
E:\GoStudy\src\HighBase\Escape>go build -gcflags "-m -m -l" ./pointer.go
# command-line-arguments
.\pointer.go:27:2: Increase capturing by ref: n (addr=false assign=true width=8)
.\pointer.go:28:9: func literal escapes to heap:
.\pointer.go:28:9: flow: ~r0 = &{storage for func literal}:
.\pointer.go:28:9: from func literal (spill) at .\pointer.go:28:9
.\pointer.go:28:9: from return func literal (return) at .\pointer.go:28:2
.\pointer.go:27:2: n escapes to heap:
.\pointer.go:27:2: flow: {storage for func literal} = &n:
.\pointer.go:27:2: from n (captured by a closure) at .\pointer.go:29:3
.\pointer.go:27:2: from n (reference) at .\pointer.go:29:3
.\pointer.go:27:2: moved to heap: n
.\pointer.go:28:9: func literal escapes to heap
.\pointer.go:36:16: in() escapes to heap:
.\pointer.go:36:16: flow: {storage for ... argument} = &{storage for in()}:
.\pointer.go:36:16: from in() (spill) at .\pointer.go:36:16
.\pointer.go:36:16: from ... argument (slice-literal-element) at .\pointer.go:36:13
.\pointer.go:36:16: flow: {heap} = {storage for ... argument}:
.\pointer.go:36:16: from ... argument (spill) at .\pointer.go:36:13
.\pointer.go:36:16: from fmt.Println(... argument...) (call parameter) at .\pointer.go:36:13
.\pointer.go:36:13: ... argument does not escape
.\pointer.go:36:16: in() escapes to heap
因为函数是指针类型,所以匿名函数当做返回值产生逃逸,匿名函数使用外部变量n,这个n会一直存在知道in被销毁
4. 变量大小不确定及栈空间不足引发逃逸
import ( "math/rand" ) func LessThan8192() { nums := make([]int, 100) // = 64KB for i := 0; i < len(nums); i++ { nums[i] = rand.Int() } } func MoreThan8192(){ nums := make([]int, 1000000) // = 64KB for i := 0; i < len(nums); i++ { nums[i] = rand.Int() } } func NonConstant() { number := 10 s := make([]int, number) for i := 0; i < len(s); i++ { s[i] = i } } func main() { NonConstant() MoreThan8192() LessThan8192() }
# command-line-arguments
.\pointer.go:43:14: make([]int, 100) does not escape
.\pointer.go:51:14: make([]int, 1000000) escapes to heap:
.\pointer.go:51:14: flow: {heap} = &{storage for make([]int, 1000000)}:
.\pointer.go:51:14: from make([]int, 1000000) (too large for stack) at .\pointer.go:51:14
.\pointer.go:51:14: make([]int, 1000000) escapes to heap
.\pointer.go:60:11: make([]int, number) escapes to heap:
.\pointer.go:60:11: flow: {heap} = &{storage for make([]int, number)}:
.\pointer.go:60:11: from make([]int, number) (non-constant size) at .\pointer.go:60:11
.\pointer.go:60:11: make([]int, number) escapes to heap
栈空间足够不会发生逃逸,但是变量过大,已经超过栈空间,会逃逸到堆上
5.总结
- 逃逸分析在编译阶段确定哪些变量可以分配在栈中,哪些变量分配在堆上
- 逃逸分析减轻了GC压力,提高程序的运行速度
- 栈上内存使用完毕不需要GC处理,堆上内存使用完毕会交给GC处理
- 函数传参时对于需要修改原对象值,或占用内存比较大的结构体,选择传指针。对于只读的占用内存较小的结构体,直接传值能够获得更好的性能
- 根据代码具体分析,尽量减少逃逸代码,减轻GC压力,提高性能
加载全部内容