2

introduction

When it comes to gold, silver and silver again, as a fat tiger who jumps once a year, he is ready to start changing jobs again, but he is taking a leave of absence for an interview today.

"Tigger" is still a bit prepared for today's interview. The eight-legged essay has been downloaded from the Internet long ago, but it was too late to memorize it and received an interview invitation. Fat Hu thought to himself, "Don't be shy, it's not for nothing that you are called Baguwen contestants. If you can scare the interviewer, you should pay more attention."

The strife has begun

Interviewer: "Students who have written C/C++ know that calling the famous malloc and new functions can allocate a piece of memory on the heap. The responsibility for the use and destruction of this piece of memory lies with the programmer. If you are not careful, it will happen. Memory leak. Then tell me how Golang handles this problem"

Fat Tiger: "Golang optimizes and simplifies memory management through escape analysis, and it can determine whether a variable is allocated to the stack."

What is escape analysis in golang

Interviewer: "Then tell me what is escape analysis?"

Fat Tiger thought: "I know this question. Are you ready? I'm going to start pretending to be X."

Golang's escape analysis means that the compiler automatically allocates variables to the heap or stack according to the characteristics and life cycle of the code.

By optimizing the memory management mechanism, the hands of programmers are liberated. Let programmers focus more on business.

Note: Go establishes escaping at compile time, not at runtime.

What is stack and heap

Interviewer: "Then what are stacks and heaps?"

Fat Tiger Heart: "This is also simple."

The stack (stack) is automatically allocated by the system. For example, we define a char a; the system will automatically create space for it on the stack. The heap (heap) is the space that the programmer applies for according to his needs, such as malloc (10); open up ten bytes of space.

First look at the memory allocation diagram

The stack is allocated from a high address down in memory, and it is continuous, following the principle of first-in, last-out. The system has already determined the size of the stack when allocating. The space on the stack is automatically reclaimed, so the life cycle of the data on the stack is released after the function ends.

Heap allocation is allocated from low addresses to high addresses, and the size of memory allocated each time may be inconsistent, resulting in discontinuous space, which also causes memory fragmentation. Since it is a program allocation, the efficiency is relatively slow. The data on the heap can always be accessed as long as the programmer does not release the space, but the disadvantage is that once it is forgotten to release, it will cause memory leaks.

Go

What are the benefits of escape analysis

Interviewer: "Then what are the benefits of escape analysis?"

Fat Tiger: "Why are you 100,000?" But Fat Tiger still took out his housekeeping skills.

As mentioned at the beginning, the allocation of memory in the Go language is not determined by the programmer, but is determined by the compilation stage. What's the benefit of this? Yes, you are witty, you may have guessed that it is to optimize the program, squeeze out the performance of the machine, and allow the memory to be used more efficiently.

Through escape analysis, those variables that do not need to be allocated to the heap are directly allocated to the stack, and fewer variables on the heap not only reduce the pressure of GC, but also reduce the overhead of memory allocation.

common escape

The interviewer nodded, looked at the fat tiger with admiration and said, "Then you are talking about the common escape phenomena, right?"

Fat Tiger's heart collapsed: "I'm talking here all the time, I'm going to die of thirst, but bring me a glass of water, can you give me a breather". But when I think that the salary given by JD is quite tempting, I am answering a question.

func (function type) data type

 package main

import "fmt"

func main() {

    name := test()

    fmt.Println(name())

}


func test() func() string {

    return func() string {

    return "后端时光"

    }
}

Excuting an order

 go build -gcflags="-m -l" eee.go

-m: Indicates memory analysis -l: Indicates that the result of preventing inline optimization is as follows, the variable name on line 11 escapes to the heap

interface{} data type

 package main


import "fmt"


func main() {
    name := "Golang"
    fmt.Println(name)
}

In the same way, the escape analysis is performed. The results are as follows. The name variable also escapes to the heap.

The reason is that in the go/src/fmt/print.go file, Println passes the parameter type interface{}, the compiler does not know the type of the variable passed in, and all unified processing is allocated to the heap.

pointer type

 package main


import "fmt"


func main() {
    name := point()
    fmt.Println(*name)
}


func point() *string {
    name := "指针"
    return &name
}

The result is as follows, the variable name on line 11 escapes to the heap

Are there any other cases where variable escapes occur?

"Uh, this..." Fat Hu thought to himself: time is too hurried, I've memorized so much of the eight-part essay, I don't have time to read the rest, it would be better if I played less games yesterday. This is how to do?

Seeing Fat Tiger's blushing face, the interviewer said with a smile, "It's alright, it's getting late, come here first, do you have anything else to ask me?"

Fat Tiger: "Which other variables will escape?"

Interviewer: "Escaping due to insufficient channel or stack space will also lead to escaping. Are there any other problems?"

Fat Tiger: "Do you rest on weekends?"

Interviewer: "Yes, we have two days off, but two days off a month"

Fat Tiger: "Ah."

Are you saying that Fat Tiger will continue to interview? Can a company like this work?

The article was first published on the public account WeChat search public account [Backend Time] Let's learn and grow together!
You have to work really hard to look effortless!

后端时光
18 声望1 粉丝