目录1.什么是内存逃逸2.什么是逃逸分析3.小结4.逃逸分析案例1.函数返回局部指针变量2.interface类型逃逸1.interface产生逃逸2.指向栈对象的指针不能在堆中3.
在一段程序中,每一个函数都会有自己的内存区域分配自己的局部变量,返回值,这些内存会由编译器在栈中进行分配,每一个函数会分配一个栈帧,在函数运行结束后销毁,但是有些变量我们想在函数运行结束后仍然使用,就需要把这个变量分配在堆上,这种从“栈”上逃逸到“堆”上的现象叫做内存逃逸
虽然Go语言引入的GC,GC机制会对堆上的对象进行管理,当某个对象不可达(没有其他对象引用他),他将会被回收。虽然GC可以降低工作人员负担,但是GC也会给程序带来性能损耗,当堆内存上有大量的堆内存对象,就会给GC很大的压力,虽然Go语言使用的是标记清除算法,并且在此基础上使用了三色标记法和写屏障技术,但是我们在堆上分配大量内存,仍然会对GC造成很大压力,Go引入了逃逸分析,就是想减少堆内存的分配,可以在栈分配的内存尽量分配在栈上
逃逸分析就是在程序编译阶段根据代码中的数据流,对代码中哪些变量需要在栈上分配,哪些需要在对象分配的静态分析方法,堆和栈相比,堆适合分配不可预知大小的内存,但是付出代价是分配速度慢,容易产生碎片,栈分配十分快,栈分配只需要两个指令“Push”和"Release"分配和释放,而且堆分配需要先找一块适合大小的内存块分配,需要垃圾回收释放,所以逃逸分析可以更好的做内存分配
Go语言的逃逸分析
src/cmd/compile/internal/gc/escape.go
既然逃逸分析是在编译阶段进行的,那我们就可以通过go build -gcflga '-m -m l'查看逃逸分析结果
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执行结束,对应栈帧就会销毁,但是引用返回到函数外部,如果我们外部解析地址,就会导致程序访问非法内存,所以经过编辑器分析过后将其在堆上分配
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{},那么编译期间就很难确定参数类型
我们把代码改成这样
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也会分配到堆上
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被销毁
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
栈空间足够不会发生逃逸,但是变量过大,已经超过栈空间,会逃逸到堆上
到此这篇关于浅谈golang内存逃逸 的文章就介绍到这了,更多相关Golang内存逃逸 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 浅谈Golang内存逃逸
本文链接: https://lsjlt.com/news/120980.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-05
2024-04-05
2024-04-05
2024-04-04
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-04
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0