欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > Golang——逃逸分析

Golang——逃逸分析

2024/10/26 1:33:19 来源:https://blog.csdn.net/wangye135/article/details/140891454  浏览:    关键词:Golang——逃逸分析

逃逸分析是指由编译器决定内存分配到堆上还是栈上。当我们在函数中申请了一个新的对象:

  • 如果分配到栈中,则函数执行结束后可自动将内存回收。
  • 如果分配到堆中,则函数执行结束后,不会自动将内存释放掉,需要GC在进行清除。

逃逸分析使Go语言能够将局部变量返回,程序员不需要担心局部变量分配到栈上还是堆上。并且Go程序员不需要像C程序员一样,担心内存泄漏的问题。

逃逸策略:

在函数中申请一个新的对象时,编译器会根据该对象是否被函数外部引用来确定是否逃逸。

  • 如果变量在函数外部没有引用,则优先放到栈上。
  • 如果变量在函数外部存在引用,则必定放到堆上。

注意,对于一个仅在函数内部使用的变量,也有可能放到堆上,比如内存过大超过栈的存储能力。我们很难通过肉眼直接判断函数内部的变量是否发生了逃逸,我们需要借助go build -gcflags=-m指令判断是否发生逃逸分析。

逃逸场景:

1.函数返回指针,会发生指针逃逸。如果我们的返回的结构体中包含指针,也会发生逃逸,比如slice、 map。

func main() {getNum()
}
func getNum() *int {i := 1return &i
}# ExpertProgramming/chapter04/escape/move_to_heap
./main.go:6:6: can inline getNum
./main.go:3:6: can inline main
./main.go:4:8: inlining call to getNum
./main.go:7:2: moved to heap: i

2.栈空间不足以存放当前创建的对象时,会将新创建的对象分配到堆上,发生逃逸。

func main() {s := make([]int, 1000000, 1000001)for index, value := range s {fmt.Println(index, value)}
}./main.go:10:14: inlining call to fmt.Println
./main.go:8:11: make([]int, 1000000, 1000001) escapes to heap
./main.go:10:14: ... argument does not escape
./main.go:10:15: index escapes to heap
./main.go:10:22: value escapes to heap

3.在编译期间很难确定其对象的具体类型时,会发生逃逸。比如参数为interface类型的函数

func main() {s := "wang"fmt.Println(s)
}./main.go:9:13: inlining call to fmt.Println
./main.go:9:13: ... argument does not escape
./main.go:9:14: s escapes to heap

4.闭包的引用对象

func main() {num := Fibonacci()for i := 0; i < 10; i++ {fmt.Println(num())}
}func Fibonacci() func() int {a, b := 0, 1return func() int {a, b = b, a+breturn a}
}./main.go:10:14: ... argument does not escape
./main.go:10:18: ~R0 escapes to heap
./main.go:15:2: moved to heap: a
./main.go:15:5: moved to heap: b
./main.go:16:9: func literal escapes to heap

5.切片长度不确定的时候也会发生逃逸,即使这时候我们不返回切片

func main() {getSlice()
}func getSlice() {length := 3s1 := make([]int, length)s1[0] = 9
}./main.go:7:6: can inline getSlice
./main.go:3:6: can inline main
./main.go:4:10: inlining call to getSlice
./main.go:4:10: make([]int, length) escapes to heap
./main.go:9:12: make([]int, length) escapes to heap

总结:

逃逸分析的目的是为了确定对象分配到了堆上还是分配到了栈上。有了逃逸分析,我们可以非常方便地返回函数里面的变量,这方便了我们的开发,但也有可能会造成我们程序的性能下降。因为分配到堆上的变量,需要GC进行回收。而GC运行的时候,会发生STW。所以,我们要减少不合理的逃逸现象,减轻GC的负担。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com