Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Escape in Go language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to understand the escape in Go language, the content is very detailed, interested friends can refer to, hope to be helpful to you.

I. what is escape?

Before we start talking about escape, let's take a look at the following two examples.

The fun () of the example 1:stack.go returns an int variable.

The fun () of the example 2:mem.go returns the * int variable, and the return value inside is & I.

The source code is as follows:

$go tool compile-S stack.go / / generate assembly statement

Compilation result analysis: through the assembly, we can see that the variable I in fun () in mem.go is the data generated by newobject (XX), which shows that this I is stored in the alignment.

Note: the definition of the newobject (XX) function is as follows:

Seeing the above example, do you wonder why the I in mem.go 's fun () function is obviously a variable but is stored in the heap?

This is actually the escape of the Go language, and the compiler performs static code analysis to determine whether a variable should be assigned to a stack or need to escape to a heap.

Second, why do you need to escape

Before analyzing escapes, we need to take a look at the heap in the Go language.

In the GE language, the heap is used as the secondary storage location, and Go will give priority to storing the data in the stack. The heap does not release allocated memory on its own. It needs to be reclaimed through GC (garbage collector), that is, the garbage collector.

Stack data in Go cannot be used as a storage location for pointers. The reason is that the stack of goroutine will point to different blocks when the stack is expanded or shrunk. Examples are as follows:

(excerpt from: https://play.golang.org/p/pxn5u4EBSI)

Once the pointer points to this stack storage location, an exception will occur at run time, and the Go compiler will become more complex to solve this problem, so the pointer to Go cannot point to the storage address in the stack.

I think this is also the reason why the data escaped by Go is stored in the heap.

Third, how is escape realized?

Take mem.go as an example, as shown below:

Output: / /. / mem

Execute $go build-gcflags "- m-m" mem.go to get the following analysis result:

Result analysis: from the output, we can see that the I of line 10 will be assigned to the heap based on the return & I of line 12.

The escaped memory allocation is as follows:

1. Main function and fun function will have two stack information, main frame and fun frame, respectively, as shown in the following figure.

two。 In fun frame, the variable I assigns the corresponding data in heap, the address is 0xc000014080, and the variable value is 0 at this time.

After calling fun (), the 3.main function copy a value of I to the variable a, where the address of an is 0xc0000c028, and the stored value is the address of I, 0xc000014080, which is in the heap.

4. Whether in fun or in the main function, the operation address 0xc000014080 can get the corresponding value.

On how to understand the escape in the Go language to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report