Analyze 5 features that make the Go language efficient (3/5): garbage collection mechanism

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Translate the original link reprint/reprint please indicate the source

English original link published in 2014/06/07

garbage collection mechanism (garbage Collection)

The go language is easier and more secure because of the enforced memory garbage collection mechanism. This does not mean that the garbage collection mechanism slows down the Go program, or that the garbage collection mechanism ultimately determines the speed of your program. There is no denying the cost of allocating memory on the heap. Each garbage collection mechanism trigger consumes a certain amount of CPU. Unless the memory is freed, these costs are unavoidable.

But there's another place we can use to allocate memory. That's the stack.

Unlike the C language, the go language does not require you to choose whether a variable is allocated on the heap (via malloc) or on the stack (by defining the variable as a local variable within the function). The go language implements an optimization technique called escape analysis .

Escape analysis can determine if any references to function local variables are also used outside of this function. If no reference is used outside of the function, then this local variable can be safely stored on the stack. Variables stored on the stack do not require special allocation and deallocation operations. Let's take a look at the following example:

The SUM function adds a number from 1 to 100 and returns the result. We don't usually write this function this way, we just use it to show the escape analysis algorithm.

Because the numbers variable is only referenced in the SUM function, the compiler stores the 100 integers on the stack instead of on the heap. So there is no need to go to garbage collection numbers variable, it will be automatically released when the SUM function returns.

The second example shown above is also created to illustrate the same problem. In the Centercursor function, we create a new cursor structure and store its pointer in variable C. Then we pass the variable C into the center () function. The Center () function translates the cursor structure by half the distance of the screen. Finally, the coordinates of X and y of the cursor structure are printed out. Although variable C is assigned through the new function, it is not stored on the heap. This is because the reference to the variable C is not used outside of the centercursor function.

The Go language optimization is always turned on by default settings. You can use the-gcflags=-m parameter to view the compiler's escape analysis and inline decisions.

Escape analysis is done at compile time and not at runtime. Regardless of how efficient the garbage collection mechanism is, the allocation of stacks is always faster than the heap allocation. Next, I'll discuss the stack.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.