标签:读写 效率 str make 代码 申请 c++ c语言 堆和栈
逃逸分析是golang编译器分析一个对象到底应该放到堆内存上,还是栈内存上(引用了他人的文章)
因为对一个程序来说,使用栈内存还是堆内存他们的效率差别很大。
栈内存:
ulimit -s
查看,目前我的实验环境是ubuntu20.04,栈内存的最大值是8M)堆内存:
根据堆和栈各自的优缺点后,逃逸分析存在的目的如下:
在Go中通过逃逸分析日志来确定变量是否逃逸,开启逃逸分析日志:
go run -gcflags ‘-m -l‘ stack.go # stack.go 来自于上一篇栈结构golang的实现
# command-line-arguments
./stack.go:17:3: &Stack literal escapes to heap
./stack.go:15:18: make([]int, size) escapes to heap
./stack.go:40:7: (*Stack).IsFull s does not escape
./stack.go:21:7: (*Stack).Push s does not escape
./stack.go:47:7: (*Stack).IsEmpty s does not escape
./stack.go:30:7: (*Stack).Pop s does not escape
./stack.go:56:13: main ... argument does not escape
./stack.go:56:13: .autotmp_1 escapes to heap
./stack.go:56:13: .autotmp_2 escapes to heap
./stack.go:62:13: main ... argument does not escape
./stack.go:62:13: .autotmp_3 escapes to heap
./stack.go:62:13: .autotmp_4 escapes to heap
./stack.go:63:13: main ... argument does not escape
./stack.go:63:13: .autotmp_5 escapes to heap
./stack.go:63:13: .autotmp_6 escapes to heap
./stack.go:64:13: main ... argument does not escape
./stack.go:64:13: .autotmp_7 escapes to heap
./stack.go:64:13: .autotmp_8 escapes to heap
false 0
true 3
true 2
true 1
其中escapes to heap
表示分配到了堆内存上。
其中does not escape
表示分配到了栈内存上。
标签:读写 效率 str make 代码 申请 c++ c语言 堆和栈
原文地址:https://www.cnblogs.com/jackey2015/p/13143249.html