Translation: Efficient garbage collector (2)

Source: Internet
Author: User

This articleArticleLet's talk about the different working modes of GC and how each mode works different from each other, so that you can understand your application.ProgramHow to select the working mode.

GC working mode so far:

1) Disable concurrent workstation GC
2) Enable concurrent workstation GC
3) server GC

If you are writing an independent hosting program without any configuration, you use the GC working mode to enable concurrent workstation GC. Most people may be surprised at this because concurrent GC is not mentioned in our document, sometimes the concurrent GC is called "background GC" (while referring working GC as "foreground GC ").

If your program runs in the Host Program, the host may select the GC working mode for the program.

NOTE: If your program is configured as a server GC, your program runs on a high-performance machine, in fact, your server runs in the "Disable concurrent job GC" mode. Because "disabling concurrent workstation GC" optimizes the high-throughput service of high-performance servers.

The design objectives of each GC mode:

1) disabling concurrent workstation GC optimizes the high throughput of high-performance servers. We perform Dynamic Optimization Based on the allocation and resurrection modes during garbage collection, so we can automatically optimize the GC efficiency when the program runs.
2) enabling concurrent workstation GC is designed for interactive applications requiring precise response time. Enabling concurrency shortens the pause time of the worker process caused by garbage collection. The goal is to use some memory and CPU, so in this mode, garbage collection requires a little more work and requires a little longer recovery time.
3) server GC. We can see from the name that this working mode is designed for server applications. A typical scenario is that you have a worker thread pool where these threads sit for similar processing. For example, you can process the same request or transaction of the same type. All threads use almost the same allocation mode. Server GC is designed for high-throughput and high-scalability multi-processor servers.

How each GC mode works:

Let's start with disabling concurrent workstation GC. the execution process is as follows:
1) memory allocation by a hosting Process
2) allocate all available memory (I will explain what this means)
3) garbage collection is triggered, and the garbage collection operation is run on the allocation thread.
4) GC calls suincludee to suspend all managed threads.
5) Start GC
6) GC calls restartee to restart the work hosting Process
7) The managed process continues to run

You can see that all the managed threads in step 1 stop the execution and wait for the garbage collection to complete the work. Sushortdee will not suspend the local thread (native threads ).
Each generation in GC has a concept of "budget allocation. The "budget allocation" of each generation is dynamically adjusted at runtime. Because we often allocate resources on The 0th generation, you can imagine that the 0th generation budget overspending will trigger garbage collection. The budget here is not the same as the size of the GC heap segment. The budget is much smaller than the segment size.

CLR garbage collection can be used to move memory or not. It is also called "cleaning" when you do not move it. The cleaning cost is lower than the compression cost because it does not need to copy and move the memory.

When concurrent garbage collection (concurrent GC) is enabled, the biggest difference is suspension and restart. I mentioned earlier that concurrent GC allows less pause time. Therefore, enabling concurrent garbage collection will reduce the execution time as much as possible. You can run and allocate memory if you need to host the thread for the remaining time. When enabling concurrency, we will allocate a large budget to the 0 generation at the beginning to ensure sufficient space for allocation objects during garbage collection. Even so, if the hosting thread needs to allocate too much memory in the concurrent reclaim operation, the thread will be blocked until the collection is complete.

Remember that the 0-and 1-generation recovery is very fast, so no concurrent recovery will be performed when the 0-and 1-generation recovery is performed. We only recycle data concurrently in the 2nd generation. If we decide to perform two-generation recovery, we will decide whether to perform concurrent recovery.

Server garbage collection, which is completely different from the first two. We will create a recycle thread for each CPU. Garbage collection is executed on these threads rather than on the allocation thread. The workflow is as follows:
1. A hosting thread for recycling
2. allocated to the threshold
3. Send a signal to the GC thread for garbage collection. Wait until the collection ends.
4. When the GC thread is running, the signal of collection completion is sent at the end (during the collection process, all managed threads will be suspended as in workstation Mode)
5. The hosting thread receives a signal and starts running again.

If you configure the garbage collection mode:
To disable concurrent recovery, add the following configuration items to the configuration file:

 
<Configuration> <runtime> <gcconcurrent enabled = "false"/> </runtime> </configuration>

To use server GC, use the following Configuration:

 
<Configuration> <runtime> <gcserver enabled = "true"/> </runtime> </configuration>

For the two configuration sections, refer to msdn.

Original article:
Http://blogs.msdn.com/ B /maoni/archive/2004/09/25/234273.aspx

Original Author: maoni Stephen ens

Related essays:

Make the Garbage Collector work efficiently (1)

. Net garbage collection mechanism principle (1)

. Net garbage collection mechanism principle (2)

. Net garbage collection and big object processing

More reference resources:
Http://blogs.msdn.com/ B /tess/archive/2008/04/17/how-does-the-gc-work-and-what-are-the-sizes-of-the-different-generations.aspx
Http://odetocode.com/Blogs/scott/archive/2004/07/16/server-or-workstation-garbage-collection.aspx
Http://msdn.microsoft.com/en-us/library/yhwwzef8.aspx
Http://msdn.microsoft.com/en-us/library/ms229357.aspx

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.