What's new in the. net clr 4.0/4.5 GC (New garbage collection mechanism in. Net 4/4. 5)

Source: Internet
Author: User

What's new in the CLR 4.0/4.5 GC?

Background and foreground GC in. Net 4

From CLR 1.0, there are two types of Garbage Collector garbage collector.

1. workstation mode GC, also known as concurrent GC [concurrent GC], is DesktopProgramThe garbage collection method.

2. The server mode GC, also called non-concurrent GC [non-concurrent GC], is the garbage collection method prepared for the server program.

Concurrent GC is run in a dedicated thread. When garbage collection is executed, other managed threads will continue to run. Other managed threads will be suspended twice during the garbage collection process, rather than being suspended during the whole time of garbage collection. The managed thread can continue to create objects in the memory (Temporary memory segment ephemeral segments) within the period that has not been suspended, the memory here refers to the ephemeral segments memory segment reserved at the beginning of the program running (specifically before GC. When memory is allocated to different objects within these reserved memory segments until the reserved memory is used up, the managed threads will be blocked. When the concurrent GC is completed, the managed thread resumes running. After the GC is completed, the concurrent GC thread suspends itself. This means that GC garbage collection will not cause latency as long as the reserved ephemeral segments memory is not exhausted.

The main difference between non-concurrent GC is that it suspends all other managed threads and does not allow memory allocation during GC garbage collection. In addition, each CPU has a dedicated GC garbage collection thread on the server GC. A few cores of CPU have several GC threads. In this mode, each CPU also creates a heap, that is, each CPU kernel has an independent heap and a GC thread.

The difference between the two garbage collection methods is mainly the difference in response time for performing garbage collection.

Non-concurrent GC blocks other threads when performing garbage collection, and other threads are suspended. Non-concurrent GC does not care much about the real-time response time. Although it is a GC on the server side, the response time is of course very important to the server, but the real-time response time is not the main factor, it is more concerned with higher throughput and reliability.

Non-concurrent GC takes less time. Concurrent GC slows down the garbage collection process.

If non-concurrent GC is used in the desktop program, that is, GC in the server mode, the desktop graphics program may flash the interface, and the response delay may occur. This phenomenon is like the android program and the iPhone program for the user experience, because the android Java program always has a garbage collection process, the UI thread may be blocked, the interface is not as smooth as the iPhone.

The main defect of concurrent GC is that in the clr1.0 era, there are few heap hosts with GB bytes in the program, at this time, the waiting time for hanging other managed threads during GC garbage collection is tolerable.

GC garbage collection methods for collecting various stacks of Gen 0, Gen 1, Gen 2, and LOH are divided:

1. Temporary garbage collection ephemeral GC: only garbage collection occurs in Gen 0 and Gen 1.

2. Full garbage collection full GC: When Gen 2 or above occurs, it becomes full garbage collection.


Gen 0, Gen 1, and Gen 2 are called small object heap (SOH). The objects in them are all smaller than 85000 bytes. The other is the large object heap, large object heap (LOH) the objects in the heap are larger than 85000 bytes, and may contain objects smaller than 85000 bytes. These objects are created by CLR operation.

The memory allocated in the managed heap is in a continuous memory. The memory is allocated directly by increasing the memory address pointer and clearing the corresponding memory area. Basically, the memory is allocated in a continuous block. This is very efficient. This is very different from the memory allocation method in windows. In Windows, the memory allocated is executed through the windows heap manager. When a request is allocated, windows heap Manager uses any idle memory in the memory to meet the requirements, which may cause the memory to be non-consecutive. When a Windows system runs many similar programs and executes many similar requests, memory fragments will appear, and the allocation efficiency will decrease as the Program increases. This is why the efficiency of using the. NET hosting program on the server is still relatively good, with the advantage of fast continuous block memory allocation.

When garbage collection occurs,

1. In Gen 0 and Gen 1, memory compression compact is used to remove dead objects, move active objects, and compress the memory so that continuous idle memory blocks are generated. Memory collection in these two heaps is a temporary garbage collection ephemeral GC. The CLR assumes that most objects are temporarily active. So put them in Gen 0. Garbage collection in Gen 0 is also very efficient, within 1 ms. In Gen 0: Gen 1, the garbage collection ratio is about, that is, 10 Gen 0 collection times. One Gen 1 collection is considered reasonable, program performance will not be too bad. In the desktop program, in the concurrent GC mode, the garbage collection of heap Gen 0 and Gen 1 is actually a way to block other managed processes (similar to the non-Concurrent Server mode ). Ephemeral GC is always a blocking process.

2. gen 2 is slightly different. The CLR manages Gen 2 by allocating more memory segments. When the objects in Gen 2 are recycled, the CLR heap manager recycles these memory segments, instead of completely releasing a memory segment, it calls the virtualalloc and virtualfree functions of the system to allocate and release it. However, because the cost of full allocation and recovery is relatively high, clr2.0 will not actually release the memory segment to the operating system, but is ready to retain the memory segment. So in the performance counter, observe the size of Gen 2 and find that even if memory is recycled, the indicator curve of Gen 2 still does not decrease, and the actual program does not use that much memory. In this generation, if Gen 2 has garbage collection, Gen 0 and Gen 1 will also have garbage collection, so it is said that the garbage collection in these three generations has become a full garbage collection.

3. the LOH heap can be regarded as an extension of Gen 2. The garbage collection of this LOH will only take place after Gen 2 garbage collection. Loh garbage collection is only performed in full garbage collection. This is a big object.Memory tighteningCompactThe operation cost is proportional to the object size. The larger the object, the higher the overhead of the tightening operation.Therefore, LOH operations are not compact operations, but idle lists are used to manage idle memory. The idle memory will not be merged (the adjacent idle memory will still be merged). This process is called cleaning.

In clr2.0, concurrent GC and non-concurrent GC are implemented in mscorwks. dll.

In clr4.0, mscorwks. dll is changed to CLR. dll, And the GC implementation mode is in this DLL.


The background GC is introduced in CLR 4.0.Background GC.

When a program activity, such as allocating memory to objects, or modify the object reference address-it is very efficient to operate on the hosted heap when the activity is not very high or the managed heap is not very large.

In clr4.0Background GCOne major difference is that it allows GC and Memory Allocation Operations at the same time, and allows ephemeral GC garbage collection memory allocation on background GC and gen 0 and Gen 1 to run at the same time. At this time, the ephemeral GC is also called the foreground GC.

To put it bluntly, background GC is the concurrent GC in the previous clr2.0 period. In addition, a foreground GC is introduced. Therefore, the focus of CLR 4.0 GC is the foreground GC of foreground GC.

Here we declare a concept: Garbage collection must occur when memory is allocated. When there is a need for memory allocation, find the idle memory. If the memory is insufficient, garbage collection will occur. So no garbage collection will occur when you close the program.

Here, let's review concurrent GC:


Indicates the current heap.

When you need to execute a full garbage collection full GC, the GC thread will scan Gen 2 and mark the dead object; by scanning the stack, the root object reference list (Root) finalize and handle are used to mark dead objects. At this time, in the concurrent GC mode, the managed thread can still allocate objects in Gen 0 and Gen 1 with sufficient memory. For example:


When the reserved memory is exhausted and there are still memory allocation requests, the EE execution engine will be suspended by the GC thread, and all the managed threads will be suspended. At this time, the application will be delayed.


As shown in, the program performance is affected at this time. One GC thread cannot process the preceding two operations at the same time. Currently, the size of ephemeral segment is 16 Mb. Before 16 Mb is used up, the program can always run. When 16 Mb is used up, then the new assigned task will wait. Performance problems may occur when GC. Collect () is manually called.

Now the foreground GC front-end GC is available. The working method of the background GC is the same as described above. The foreground GC is triggered when the B-GC executes an Gen 2 garbage collection and gen 0 and Gen 1 (that is, ephemeral segment) need to expand memory. For example:


This ephemeral foreground thread, the temporary front-end thread will mark the dead object and adopt the segment switching (SWAP segments) method, instead of raising the gen 0 or Gen 1 object to a higher Gen, the ephemeral segment is empty, the exchanged Gen 1 object will become a new Gen 2 object.


With this front-end GC, the application can start object creation and allocation without waiting for full GC to complete.

When the current GC is running, the background GC and other managed threadsShouldWill be blocked. They will continue to run only when the GC at the front end is complete.

In clr4.0, this method can be used by the workstation app of the desktop program. Background GC and foreground GC are implemented in CLR 4.5.




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.