Garbage collection-generation

Source: Internet
Author: User

Garbage collection-generation
The proxy is a mechanism adopted by the CLR garbage collector. its only purpose is to improve the application.ProgramPerformance, the use of the garbage collector to do the following:
1: The newer the object, the shorter the lifecycleIt is similar to the stack principle. The local variables defined first after the first time are used to stay in the stack for a relatively long time.
2: The older the object, the longer the lifecycle, Which will be explained later.
3: reclaim part of the heap, which is faster than the whole heap.That is to be affirmed, that is, to realize that only a portion of the data in the memory is recycled, the concept of generation is generated, and only 0th generations are recycled most of the time.

 

0th generations: The managed heap does not contain any objects during initialization. The newly allocated object on the heap is called The 0th generation. The Garbage Collector never checks the object and the CLR initializes the object.

A capacity is budgeted for The 0th generation, which is assumed to be kb. The capacity is generally an integer multiple of KB, which is related to the CPU L2 cache capacity.

It takes 0th kb, 1 Mbit/s to load all data in the generation into the L2 cache of the CPU, which makes data processing faster.

 

1st generations: If a new object is allocated and the data in the 0th generation exceeds its budget, garbage collection will occur, and data that cannot be reached will be recycled.

The first generation has a larger capacity than the second generation, and is assumed to have not been referenced by any object.

2 M. After a garbage collection, The 0th generation does not contain any data. As long as the 1st generation does not exist, it will not be recycled.

The newly generated object is always allocated in The 0th generation. When the data in the 0th generation is full, garbage collection will occur, and the unrecycled objects will be compressed to the 1st generation. Data Compression is quite costly.

The performance is to copy data from the existing A to the B in the memory. As long as the data in the 1st generation is not full, the garbage collector will only recycle the data in the 0th generation. This is

Partial recycling: when the data in the 1st generation is full, the garbage collector will recycle both the 0th generation and the 1st generation. The data not recycled in The 0th generation will be compressed to the 1st generation

The 1st generation is full.

 

2nd generations: When the data in the 1st generation is full, the 1st generation will be recycled. The data that has not been recycled is called the 2nd generation. The 2nd generation has a capacity greater than 1st generations, and the managed heap only supports three generations.

, 0th, 1st, and 2nd.

 

CLR assumes that the life cycle of newly generated objects is short, so a large amount of memory can be recycled every time the first generation is recycled. Therefore, CLR is always very efficient for the first generation.

? Haha! This may cause the 0th generation to be recycled many times, and the 1st generation will not be recycled once. Of course, the garbage in the 1st generation will not be recycled. CLR assumes that the ratio of live to live is better.

A longer object can survive. The older the object, the longer the lifecycle.

 

The managed heap only partially recycles, that is, recycles 0th generations. Because the capacity of 0th generations is small, the garbage collection speed is faster each time. Of course, because the capacity of 0th generations is small

The garbage collection frequency is higher. For better garbage collection performance, The 0th-generation capacity is dynamic.

If it is recycled, the capacity of the hosting budget for 0th generations will be reduced, which will speed up the garbage collection process. If garbage collection is executed every time, a large amount of garbage is not collected.

From 0th generation to 1st generation, the managed heap will increase the budget capacity of 0th generation, which can reduce data transfer. Data transfer is equivalent to sorting out memory fragments,

The impact on program performance is still quite large.

 

Another benefit of only recycling The 0th generation is that if the data in the 0th generation references the data in the 1st generation, the data in the 1st generation does not need to be checked, and the referenced object in the 1st generation

Internal structure, the garbage collector does not care, the recovery is faster, of course, the data in the 1st generation may also reference data in the 0th generation, if the object in the 1st generation references the data in the 0th generation

Objects in the 1st generation will be marked, and only the objects marked after the previous garbage collection to the next garbage collection will be checked. Of course, the objects in the 1st generation will be checked.

Data ah, you do not check the data in the 1st generation. How do you know that the data in the 0th generation is not reachable and referenced? In the 1st generation, only the marked object will be checked

This can also improve the performance of garbage collection. In short, the garbage collector designed by Microsoft is improving the performance of your program everywhere. He said so, do you feel it?

 

If three generations of memory are still not allocable, an outofmemoryexception will be thrown, telling you that there is no memory to allocate. In the past, a project was developed.

This exception is thrown occasionally. At that time, we didn't know what was going on. The reason is that we constructed a large JSON object using string + String, resulting in memory being

If the stringbuilder is used at the time, the performance will be greatly improved, and the outofmemoryexception will not be abnormal. Hahaha.

 

Author: Chen taihan

Blog: http://www.cnblogs.com/hlxs/

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.