golang new和make使用时创建内存逃逸分析

前言

在Golang中,初始化数据结构的时候,能用到2种内置函数:new和make。
new和make都可以用来分配内存。make只用于map, slice和channel,返回的是类型的引用而不是指针。要想获得一个显示的指针,使用new进行进行分配,或者显式地使用一个变量的地址。

new是什么

官方的定义:

func new(Type) *Type

The new built-in function allocates memory. The first argument is a type, not a value, and the value returned is a pointer to a newly allocated zero value of that type.

官方的定义解释到,new的特点如下:

  1. 分配内存。内存里存的值是对应类型的0值;
  2. 只有一个参数。参数是分配的内存空间所存储的变量类型,golang的任何类型都可以是new函数的入参,int, float, array,struct,甚至函数类型都可以;
  3. 返回的是指针。

关于go的new的内存观点:

  • golang的new分配的内存可能在栈(stack)上,可能在堆(heap)上, 但多数情况下都在栈(stack)上,发生逃逸或者其他情况会在堆上,下文会有一些逃逸分析。
  • golang的new分配的内存里的值是对应类型的零值,不能显示初始化指定要分配的值。
  • golang里没有构造函数,golang的new不会去构造函数。

make是什么

make的官方定义:

func make(t Type, size ...IntegerType) Type

The make built-in function allocates and initializes an object of type slice, map, or chan (only). Like new, the first argument is a type, not a value. Unlike new, make's return type is the same as the type of its argument, not a pointer to it. The specification of the result depends on the type:

Slice: The size specifies the length. The capacity of the slice is
equal to its length. A second integer argument may be provided to
specify a different capacity; it must be no smaller than the
length. For example, make([]int, 0, 10) allocates an underlying array
of size 10 and returns a slice of length 0 and capacity 10 that is
backed by this underlying array.

Map: An empty map is allocated with enough space to hold the
specified number of elements. The size may be omitted, in which case
a small starting size is allocated.

Channel: The channel's buffer is initialized with the specified
buffer capacity. If zero, or the size is omitted, the channel is
unbuffered.

make的特点如下:

  1. 分配和初始化内存。
  2. 只能用于slice,map和chan 3种类型,不能用于其他类型。
  3. 返回是是原始类型,即slice, map和chan,不是对应的指针。

逃逸分析

逃逸分析命令:
go build -gcflags=-m
golang 版本
go version go1.21.3 linux/amd64

  1. 一个小的slice,如果单纯的使用,就是在栈空间分配

    func F() {
    	temp := make([]int, 0, 20)
    	temp[1] = 0
    }
    
  2. 但如果有发生参数传递,例如单纯一个打印

    func F() {
    	temp := make([]int, 0, 20)
    	temp[1] = 0
    	fmt.Println(temp)
    }
    

    就会逃逸到堆空间

  3. 其实也不一定,如果只是单纯的调用

    func F() {
    	temp := make([]int, 0, 20)
    	temp[1] = 0
    	F2(temp)
    }
    
    func F2(temp []int) {
    
    }
    

    这种情况也不会发生逃逸,可见是否逃逸决定于被调用函数(方法)的使用情况

  4. 如果入参有返回,肯定会发生逃逸到堆空间

    func F() []int {
    	temp := make([]int, 0, 20)
    	return temp
    }
    
  5. 栈空间充足的时候,通常会分配在栈空间

    func Slice() {
    	s := make([]int, 1000, 1000)
    
    	for index, _ := range s {
    		s[index] = index
    	}
    }
    
  6. 栈空间不足的时候,就直接分配到堆空间了(栈)

    func Slice() {
    	s := make([]int, 10000, 10000)
    
    	for index, _ := range s {
    		s[index] = index
    	}
    }
    
  7. 对于new来说,一般情况下也是直接分配到栈空间

    func F() {
    	temp := new([]int)
    	F2(temp)
    }
    
  8. 同make一样,如果new之后调用fmt.Println()也会发生逃逸情况

    func F() {
    	temp := new([]int)
    	fmt.Println(temp)
    }
    
  9. 如果new之后的指针作为返回值,也会发生逃逸

    func F() *[]int {
    	temp := new([]int)
    	return temp
    }
    
  10. 闭包内是否发生逃逸跟闭包内的参数作用情况有关

会发生逃逸
func F() {
 fc := func() []int {
 	temp := make([]int, 0, 20)
 	temp[1] = 0

 	return temp
 }
 fc()
}
不会发生逃逸
func F() {
 fc := func() {
 	temp := make([]int, 0, 20)
 	temp[1] = 0
 }
 fc()
}

总结:

一般来说,发生逃逸的几种情况:

  1. 函数内分配的指针,作为函数返回值返回的时候;
  2. 栈空间不足的时候;
  3. interface:当函数的入参是interface的时候;(这也就是上面为啥fprint要逃逸,而普通方法不逃逸,因为fprint的入参为 …any,也就是[]interface{})
  4. slice和map里面的元素是指针的时候
  5. 闭包有的文章说必然会出现逃逸,实际经过测试,并不会,闭包就是一个函数,只不过是匿名函数而已,是否逃逸,也是跟闭包内的变量是否作为返回值以及其他情况密切相关,网上很多人明明没有经过试验,就认为闭包一定会发生逃逸,误人子弟!
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值