How GC. Collect affects garbage collection

Source: Internet
Author: User

According to the garbage collection algorithm, objects are stored in the memory in a generational manner. Normally, when 0th generations are full of allocated space, for example, 256 kb ), GC will start to recycle the 0th generation object, and the surviving 0th generation object will be put into the 1st generation, and the 1st generation object will be collected until it is fully filled. Therefore, the younger generation, the more frequently it is collected. Because GC usually only collects 0th generation objects, it not only ensures that a large amount of memory can be recycled, but also ignores older generation objects, this accelerates garbage collection and improves performance.

Therefore, when gc. collect is called, it is equivalent to forcibly executing a collection for all generations, both young and old. When the Garbage Collector recycles resources, the threads that are executing the managed code will be suspended. The details are quite complex, because some threads are running at unsafe points, the CLR cannot execute garbage collection. Therefore, the CLR uses the thread hijacking technology, that is, it modifies the thread stack method for garbage collection. This complexity reduces performance. This method is considered to be called unless a large number of old objects are determined to die.

Therefore, in general, do not intervene in the work of the garbage collector, that is, do not take the initiative to call GC. Collect.

Since garbage collection is asynchronous, CLR has a dedicated thread responsible for garbage collection, even if GC is called. collect does not call Finalize in real time. Therefore, to ensure that the Destructor is indeed called, you can use the statement GC. waitForPendingFinalizers ().

The following is a piece of code.

GC. Collect ();

GC. WaitForPendingFinalizers ();

Statement.

Using System; using System. collections. generic; using System. linq; using System. text; using System. linq. expressions; using System. reflection; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {AA aa1 = new AA ("1"); AA aa2 = new AA ("2 "); AA aa3 = new AA ("3"); aa1 = null; aa2 = null; // GC. collect (); // GC. waitForPendingFinalizers (); var tmp = aa3 ;}} public class AA {public s Tring id = ""; public AA (string s) {id = s; Console. WriteLine ("Object AA _" + s + "created ");}~ AA () {Console. WriteLine (id + "destructor executed ");}}}

When a statement is commented out, although both aa1 and aa2 are set to null, garbage collection does not immediately recycle them. Objects may all be placed on The 0th generation. When the process ends, the garbage collector recycles them together. The output is as follows, and the order is 321.

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" e1373249592f420e854f3b035ddf3fd7 "border =" 0 "alt =" e1373249592f420e854f3b035ddf3fd7 "src =" http://www.bkjia.com/uploads/allimg/131229/12361K515-0.jpg "height =" 162 "/>

However, after the annotation is canceled, both aa1 and aa2 objects are null due to forced garbage collection, so they are recycled. The order is 213.

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" inherit "border =" 0 "alt =" b0b99d8b1eec4114b26e7fa195225f4b "src =" http://www.bkjia.com/uploads/allimg/131229/12361H423-1.jpg "height =" 171 "/>

NOTE: If aa1 and aa2 are not set to null, the two objects cannot be recycled when they are forcibly recycled. Therefore, it will not be recycled until the process ends.

This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/1343004

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.