c/c++的programmer对于堆内存、栈内存一定非常熟悉,以为内存管理完全由使用者自己管理。Go语言的内存管理完全由Go的runtime接管,那么是不程序员就完全不用care变量是如何分配的呢?
Go语言中局部的非指针变量通常是不受GC管理的,这种Go变量的内存分配称为“栈分配”,处于goroutine自己的栈中。由于Go编译器无法确定其生命周期,因此无法以这种方式分配内存的Go变量会逃逸到堆上,被称为 内存逃逸 。
先来说一下通过go编译器查看内存逃逸方式go build -gcflags=-m xxx.go
package main
type User struct {
Namestring
}
func foo(s string) *User {
u := new(User)
u.Name= s
return u // 方法内局部变量返回,逃逸
}
func main() {
user := foo("hui")
user.Name= "dev"
}
//# command-line-arguments
//./escape.go:7:6: can inline foo
//./escape.go:13:6: can inline main
//./escape.go:14:13: inlining call to foo
//./escape.go:7:10: leaking param: s
//./escape.go:8:10: new(User) escapes to heap
//./escape.go:14:13: new(User) does not escape
package main
import "fmt"
func main() {
name := "devhui"
fmt.Println(name)
}
//# command-line-arguments
//./escape_02.go:7:13: inlining call to fmt.Println
//./escape_02.go:7:13: name escapes to heap
//./escape_02.go:7:13: []interface {}{...} does not escape
//:1: leaking param content: .this
很多函数的参数为interface{} 空接口类型,这些都会造成逃逸。比如
func Printf(format string, a ...interface{}) (n int, err error)
func Sprintf(format string, a ...interface{}) string
func Fprint(w io.Writer, a ...interface{}) (n int, err error)
func Print(a ...interface{}) (n int, err error)
func Println(a ...interface{}) (n int, err error)
复制代码
编译期间很难确定其参数的具体类型,也能产生逃逸
func main() {
fmt.Println("hello 逃逸")
}
/* 逃逸日志分析
./main.go:5:6: can inline main
./main.go:6:13: inlining call to fmt.Println
./main.go:6:14: "hello 逃逸" escapes to heap
./main.go:6:13: []interface {} literal does not escape
*/
package main
func main() {
s := make([]int, 1000, 1000)
for index, _ := range s {
s[index] = index
s1 := make([]int, 10000, 10000)
for index, _ := range s1 {
s1[index] = index
}
}
逃逸分析:
./escape_03.go:4:11: make([]int, 1000, 1000) does not escape
./escape_03.go:9:12: make([]int, 10000, 10000) escapes to heap
s足够在栈空间分配没有逃逸;s1空间不够在栈内分配发生了逃逸。
package main
func main() {
s := make([]int, 0, 1000)
for index, _ := range s {
s[index] = index
}
num := 1000
s1 := make([]int, 0, num)
for index, _ := range s1 {
s1[index] = index
}
}
逃逸分析:
./escape_05.go:4:11: make([]int, 0, 1000) does not escape
./escape_05.go:10:12: make([]int, 0, num) escapes to heap
s分配时cap是一个常量没有发生逃逸,s1的cap是一个变量发生了逃逸。
func Increase() func() int {
n := 0
return func() int {
n++
return n
}
}
func main() {
in := Increase()
fmt.Println(in()) // 1
fmt.Println(in()) // 2
}
//./escape_04.go:6:2: moved to heap: n
//./escape_04.go:7:9: func literal escapes to heap
//./escape_04.go:7:9: func literal does not escape
//./escape_04.go:15:16: int(~R0) escapes to heap
//./escape_04.go:15:13: []interface {}{...} does not escape
//./escape_04.go:16:16: int(~R0) escapes to heap
//./escape_04.go:16:13: []interface {}{...} does not escape
//:1: leaking param content: .this
import "testing"
// sliceEscape 发生逃逸,在堆上申请切片
func sliceEscape() {
number := 10
s1 := make([]int, 0, number)
for i := 0; i < number; i++ {
s1 = append(s1, i)
}
}
// sliceNoEscape 不逃逸,限制在栈上
func sliceNoEscape() {
s1 := make([]int, 0, 10)
for i := 0; i < 10; i++ {
s1 = append(s1, i)
}
}
func BenchmarkSliceEscape(b *testing.B) {
for i := 0; i < b.N; i++ {
sliceEscape()
}
}
func BenchmarkSliceNoEscape(b *testing.B) {
for i := 0; i < b.N; i++ {
sliceNoEscape()
}
}
BenchmarkSliceEscape
BenchmarkSliceEscape-10 53271513 22.09 ns/op
BenchmarkSliceNoEscape
BenchmarkSliceNoEscape-10 187033111 6.458 ns/op
package escape_bench_02
import "testing"
type St struct {
arr [100]int
}
func retValue() St {
var st St
return st
}
func retPtr() *St {
var st St
return &st
}
func BenchmarkRetValue(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = retValue()
}
}
func BenchmarkRetPtr(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = retPtr()
}
}
BenchmarkRetValue-10 34714424 34.45 ns/op 0 B/op 0 allocs/op
BenchmarkRetPtr-10 8038676 145.3 ns/op 896 B/op 1 allocs/op
可以看到返回值更快且没有发生堆内存的分配。
package escape_bench_03
import "testing"
const capacity = 1024
func arrayFibonacci() [capacity]int {
var d [capacity]int
for i := 0; i < len(d); i++ {
if i <= 1 {
d[i] = 1
continue
}
d[i] = d[i-1] + d[i-2]
}
return d
}
func sliceFibonacci() []int {
d := make([]int, capacity)
for i := 0; i < len(d); i++ {
if i <= 1 {
d[i] = 1
continue
}
d[i] = d[i-1] + d[i-2]
}
return d
}
func BenchmarkArray(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = arrayFibonacci()
}
}
func BenchmarkSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sliceFibonacci()
}
}
BenchmarkArray-10 346110 2986 ns/op 0 B/op 0 allocs/op
BenchmarkSlice-10 389745 2849 ns/op 8192 B/op 1 allocs/op
那么多大的变量才算是小变量呢? 对 Go 编译器而言,超过一定大小的局部变量将逃逸到堆上,不同 Go 版本的大小限制可能不一样。一般是 < 64KB,局部变量将不会逃逸到堆上。
package escape_bench_04
import "testing"
const capacity = 1024
func returnArray() [capacity]int {
var arr [capacity]int
for i := 0; i < len(arr); i++ {
arr[i] = 1000
}
return arr
}
func returnInterface() interface{} {
var arr [capacity]int
for i := 0; i < len(arr); i++ {
arr[i] = 1000
}
return arr
}
func BenchmarkReturnArray(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = returnArray()
}
}
func BenchmarkReturnInterface(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = returnInterface()
}
}
页面更新:2024-03-07
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号