10. Net Discovery series-in-depth understanding of platform mechanisms and performance impact (I)

Source: Internet
Author: User
Tags compact

In a twinkling of an eye 《. the Net Discovery series has been released for one year. This article is the 10-13 article in this series. net platform knowledge to make a small summary and mechanism analysis, and introduce and focus on the impact of these mechanisms on program performance and improvement suggestions.
This article consists of four parts: garbage collection, real-time compilation, exception handling, and string resident mechanism and performance improvement suggestions. Every article in the. Net Discovery series takes more than two days to write. The famous author, Aicken (Li Ming), was asked to reprint the article and prohibited all commercial use without the consent of the author!
I. About the garbage collection mechanism
● Mechanism Analysis
The garbage collector is. A feature of the. Net platform that automatically recycles objects that are no longer used on the hosting stack and cleans up the memory in a timely manner. This is transparent to developers. Of course, you can also manually summon it, its essence is to track all referenced objects, sort out objects that are no longer referenced, and reclaim the corresponding memory. The garbage collection mechanism uses the Mark Sweep algorithm to complete the above tasks. The whole process is divided into two steps:
Step 1. mark-Sweep: Starting from the root of the application, uses the mutual reference relationship to traverse all the objects dynamically allocated on the Heap, specify the objects to be recycled, and Mark the surviving objects, mark it.
Step 2. compact: moves the surviving objects in the memory, modifies their pointers, and keeps them in the memory. In this way, idle memory is continuously released, it also solves the memory fragmentation problem. This process can also be used to compress pointers.
The garbage collector generally divides the objects in the managed heap into three generations, which can be called by GC. maxGeneration learns that objects are divided into generations based on the length of time they exist. The shortest score is 0th generations, the longest score is 2nd generations, and the objects in 2nd generations are usually relatively large, the second generation space is called Large Object Heap. For the collection of 2 generation objects, the biggest difference is that there is no compression process for pointer movement.
For example, the first GC, the first column of A-F on the left represents the object in the memory, located in the light blue area, after Mark, ACDF marked as available, the Sweep process cleared BE, Compact process moved ACDF, it is located in the continuous storage area; the second uses green for marking; the third GC uses blue for marking; it can be seen that the third GC process has no pointer moving compression process.

Figure 1 collection of objects

● Performance impact analysis
This process seems a bit complicated. It is true that the startup of the garbage collector takes some CPU time, which affects the system performance. However, this impact is very limited and these losses are worthwhile.
1. the garbage collector is not started without regularity, but is started when the current age reaches a certain trigger condition, and the garbage collector is only a resource of the first and second generations with a low age, objects in LOH are not moved. This avoids performance loss caused by long GC thread lock.
2. GC has three different working modes, which are suitable for different environments. Not all environments use the process of "suspend-> Search and Mark-> compress-> recover. In the "Workstation GC with Concurrent" mode, 0th and 1 generations of collection are still pending applications, which will be processed in parallel when 2nd generations are collected, the principle is to split the Full GC process into several transient sub-processes to freeze the thread. The application can still run normally beyond the thread freeze time. This mainly sets a large number of 0th generation space so that when Full GC is enabled, CLR can still allocate memory in 0th generation. If Full GC is enabled, the 0th generation memory is exhausted, then the application will be suspended, waiting for the completion of Full GC.
In the case of multiple CPUs, you can use the "Server GC" mode. This GC mode has high performance and efficiency. In this mode, CLR creates a dedicated GC thread for each CPU. Each CPU can independently perform GC operations for the corresponding heap. These GC threads work in a non-concurrent manner, collection tasks and threads cannot work normally at the same time, that is to say, 0th, 1, and 2 generations of collection will suspend the application thread.
In. Net 4.0, there is a new garbage collection mechanism called background collection. This mechanism is based on concurrent GC. As mentioned above, in the Workstation GC with Concurrent mode, CLR can still allocate memory in the zero generation during the Full GC process, if the 0-generation memory is used up during Full GC, the application will be suspended and the Full GC process will be completed.
3. the garbage collector works with the policy engine. The policy engine can wake up GC, which automatically starts GC based on the number, frequency, and age of GC startup. It is particularly important to note that manual calls by programmers to the Code collected by GC will also affect the work of the Policy engine, which will give the policy engine an error signal, therefore, GC is not recommended unless necessary. collect (); manually recycle.
● Summary
Compared with the performance loss caused by the garbage collector, we should focus on program optimization, pay attention to the release of unmanaged resources, String concatenation, and business code in the loop. The garbage collection mechanism is not. net is not a Java patent. It has an evolutionary history. More and more cases prove the advantages of the garbage collection mechanism. Most modules of Exchange 2010 are based on the hosted environment.
Ii. Real-time compilation Mechanism
JIT (Just In Time) is. net is a mechanism for compiling while running. This mechanism is named by a production method implemented by Toyota in 1960s ".
. Net JIT compiler, in terms of its original design intention and running method, is very similar to the "punctual production" system of Toyota Motor, so let's first understand it through "punctual production. net JIT mechanism.
The basic idea of "punctual production" can be summarized as "the products required for mass production as needed", which is exactly the same. net JIT compiler is designed to compile the required code when needed.

Unfinished

In the next article, Aicken will introduce the. Net JIT mechanism and its performance problems in combination with Windbg.

I'm Aicken (Li Ming). Please stay tuned to my next article.

 

PS: Why did the original editor disappear when I sent the essay? Instead, there is only an HTML editing interface. In addition, where can I choose the "homepage publishing option" in the blog garden for publishing Windows live logs "? This year's progress is puzzling!

The ". Net Discovery series" is an article that explains the nature of the. Net platform. It now includes:

7 In the. Net Discovery series-an in-depth understanding of the. Net garbage collection mechanism (garbage collection) was released in the first second of the New Year.

. Net Discovery series 5-Me JIT (in simple terms)

6 of the. Net Discovery series-Me JIT (in brief, under. Net JIT)

. Net Discovery series 3-deep understanding of. Net garbage collection mechanism (I)

. Net Discovery series 4-deep understanding of. Net garbage collection mechanism (II)

One of the. Net Discovery series-string from entry to mastery (on)

. Net Discovery series II-string from entry to mastery (lower)

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.