Garbage collection mechanism to be understood (1)

Source: Internet
Author: User
As a C ++ C # Program Clerk, I initially had a skeptical attitude towards garbage collection (GC) and wondered if it could operate stably and efficiently. Now, I have to say that I have gradually become accustomed to and rely on GC and my program to "run together", and the keyword "delete", which is used to serving as the culprit, has gradually become unfamiliar. However, practice has proved that my excessive trust in GC has led to many unexpected errors, which has inspired me to gain a deeper understanding of GC operating machine production. Then I began to read the book, check the information, and finally had a complete understanding of GC (but far from being in-depth ). Some people may say, "Is there any value to study the internal mechanism of GC? We develop applications. The customer's machines can achieve high configuration, and the memory resources are not a problem ." This statement clearly states that" Garbage collection = memory release "In fact, in garbage collection, the most troublesome is not the amount of memory, but the memory is released, GC secretly performs complicated transactions for US (for example, cleaning and releasing of unmanaged resources ). If you do not know the basic operations of GC, but you do not have time to carefully read many Technical materials Then, my articles Article It may be helpful to you.

The following describes how to allocate and release resources.

 

I.Allocate managed resources

CLRA memory address space (virtual address space, which is mapped to the physical memory address during running) is managed at runtime, which is divided into two parts: "managed heap" and "stack, stack is used to store value type data. It will automatically destroy the referenced value type variables after the method is executed. This part is not within the scope of garbage collection. Managed heap is used to store referenced variables, which is a key position in garbage collection.

The managed heap is a continuous address space, and the allocated space shows a queue structure similar to an array:

 NextobjptrIs a memory pointer maintained by the managed heap, indicating the starting address of the memory allocated by the next object, it will continue to move with the memory allocation (of course it will also move with the memory garbage collection), always pointing to the next idle address. 

Here, we may wishC ++Compare the efficiency of the memory allocation mechanism (skip if you are not interested in efficiency) ), By the wayC ++Some of my friends dismissedCLRMemory Allocation Efficiency concerns. When searching for idle memory space,CLRYou only needNextobjptrThe specified size of space is directly reserved for data initialization, and a new idle address is calculated and reset.NextobjptrPointer. InC/C ++Before allocating memory, traverse the linked list occupied by memory to find memory blocks of the proper size, and then modify the linked list. In this way, memory fragments are easily generated, this reduces the Memory Allocation performance. Obviously,. NetIs more efficient. However, this efficiency isGC. 

II.Spam Determination

To collect garbage, you must first know what garbage is.GCSearch for spam by traversing the "root" in the application. We can think that the root is a pointer to the memory address of the reference type object. If an object has no root, that is, it is no longer referenced by any position, then it is a candidate for garbage.

1 Public   Static   Void Main ()
2 {
3 String Sgarbage =   " I'm here " ;
4
5 // The followingCodeWithout referencing s, it has become a spam object-of course, such code itself is also spam;
6 // At this time, if garbage collection is executed, the sgarbage may have been sent to another day.
7
8 Console. writeline ( " Main () is end " );
9 }

It is worth noting that objects may be listed in the spam list before their lifetime ends, or evenGCAssassination! This is because the object may no longer be referenced at a certain time in the lifetime. If garbage collection is executed at this time, this unfortunate object is veryPossibleIt has been marked as garbage and destroyed (why is it "possible? Because it is not necessarilyGCWithin the visual acuity range. We will introduce the relevant details later on "age generation ). 

3..Object age generation

although GC we work, but after all, it is created by people. People will be lazy, and they will. In order to reduce the workload, it always wants to reduce the scope of work; it firmly believes that objects created later tend to be short-lived, so it will focus on the memory area of this part, hold other parts temporarily. GC introduces the concept of "age generation" to divide the object survival level.

CLRThe first batch of created objects after Initialization is listed0Object.CLRWill be0When the size of the created object exceeds the configured capacity limit,GCIt starts to work. The scope of work is0On behalf of the object's memory area, and then began to search for junk objects, and release the memory. WhenGCAfter work, the surviving objects will be listed1Objects are retained in1Within the region of the object. The newly created objects will be listed as a new batch.0Generation object0The memory area of the generation is filled up again, and then0A new round of garbage collection is carried out on behalf of the object region.0The object will be listed as the first1Object substitution, incorporated into1Within the region. The1At the beginning, the generation region will also be set to a capacity limit value.1After the size of the Substitute Object exceeds this limit,GCThe battlefield will be expanded.1Garbage collection is also performed on behalf of the Region. After that, the surviving objects will be upgraded to a New Age2Object. 

 It can be seen that some objects comply with all the garbage conditions, but if they are1Generation (or even2And1When the allocation volume of the generation is smaller than the set limit value, these junk objects will not beGCAnd can survive.

In addition,GCWe will also learn from the experience during the work process and automatically adjust the capacity of each generation object region based on the characteristics of the application, so as to work more efficiently.

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.