. Net garbage collection mechanism-Generation Principle Analysis

Source: Internet
Author: User

The. NET Garbage Collector uses a generation principle. If the following three conditions are true, the concept of generation is proposed.

  • The newer the object, the shorter its lifecycle.
  • The older the object, the longer its lifecycle
  • It is better to reclaim some heap objects than to recycle all the heap objects.

Step 1: when declaring the A-E's five objects, they are allocated in the stack (garbage collection mechanism didn't scan before), later, C and E objects become unreachable

Step 2: When CLR is initialized, it allocates K space (may be different) for the nth generation. The higher the generation, the more memory allocated. Assuming that the A-E is allocated with K of memory occupied, when the f object is declared, the recycler will perform the nth generation of recovery, C and E are recycled, A, B, D is promoted to the next generation.

Step 3: The F-K object is assigned to the nth generation, and later, B, H, and J are not reachable .,

Step 4: But the Garbage Collector checks the space of the first generation. If there is enough space, the first generation of garbage collection will be ignored and only the first generation of space will be recycled.

Step 5: continue to apply for L-O, at the same time, as the application runs G, L, M is not reachable,

Step 6: If you continue to apply for P, if The 0th generation space is insufficient, but the 1st generation space is sufficient, only the 0th generation will be reclaimed. If the 1st generation space is ignored, the garbage collector will recycle it later:

Step 7: and continue to allocate the space of the P-to-s object:

Step 8: when the T object is allocated, the first and second generations of the garbage collection period will be scanned and the second generation will be found to be insufficient space. At this time, the space of the first and second generations of garbage collection will be started: at this time, the object of the 0th generation is upgraded to the 1st generation, and the object of the 1st generation is upgraded to the 2nd generation.

The garbage collection mechanism has only three generations. When CLR initialization, it allocates approximately 0th K space for the 1st generation, the 2nd generation is about 2 m, and the generation is about 10 m, based on this assumption, the performance of the application can be improved. It is important to know that the garbage collection mechanism affects the performance.

Internal static class gcmethods <br/>{< br/> Public static void go () <br/>{< br/> console. writeline ("Maximum generations:" + GC. maxgeneration); </P> <p> // create a new genobj In the heap. <br/> Object o = new genobj (); </P> <p> // because this object is newly created, it is in generation 0. <br/> console. writeline ("gen" + GC. getgeneration (o); // 0 </P> <p> // specify a garbage collection promotes The object's generation. <br/> GC. collect (); <br/> console. writeline ("gen" + GC. getgeneration (o); // 1 </P> <p> GC. collect (); <br/> console. writeline ("gen" + GC. getgeneration (o); // 2 </P> <p> GC. collect (); <br/> console. writeline ("gen" + GC. getgeneration (o); // 2 (max) </P> <p> O = NULL; // destroy the strong reference to this object. </P> <p> console. writeline ("collecting Gen 0"); <br/> GC. collec T (0); // collect generation 0. <br/> GC. waitforpendingfinalizers (); // finalize is not called. </P> <p> console. writeline ("collecting Gen 1"); <br/> GC. collect (1); // collect Generation 1. <br/> GC. waitforpendingfinalizers (); // finalize is not called. </P> <p> console. writeline ("collecting Gen 2"); <br/> GC. collect (2); // same as collect () <br/> GC. waitforpendingfinalizers (); // finalize is called. <B R/>}</P> <p> internal sealed class genobj <br/>{< br/> ~ Genobj () <br/>{< br/> console. writeline ("in Finalize method"); <br/>}< br/>}

Result:

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.