. Net GC working mechanism the working principle of the garbage collection mechanism

Source: Internet
Author: User
First, many people use. Net to write data. Program To talk about the concept of hosting. So what does. Net mean by resource hosting? Is it relative to all resources, or is it limited to some resources? Many people do not know much about this, Actually . Net The hosting is only for memory, not for all resources; therefore, for stream , Database connection, GDI + And com. Objects. These resources are not affected by. net. Management. For memory release and recovery, the system provides GC-Garbage Collector Other resources must be manually released.

So the second concept is what is garbage.Article. NET is divided into two categories: Value Type and reference type. The former is allocated to the stack and does not require GC collection; the latter is allocated to the stack. Therefore, the memory release and recovery must be completed through GC. GC is called "Garbage Collector". As its name implies, it is a garbage collector. Only objects called garbage can be recycled by GC. That is to say,The memory occupied by a reference type object needs to beGCGarbage collection is required first. So. netHow can I determine that a reference type object is spam?. netAs long as it is determined that this object or its contained Sub-objects are not referenced by any reference, the system considers it as garbage.

After clarifying these two basic concepts, let's talk about how GC works and its functions. The release and recovery of memory must be accompanied by program running. Therefore, the system schedules independent threads for GC. GC generally queries whether objects in the memory become garbage and then releases and recycles the garbage. Therefore, GC takes priority over memory recovery.AlgorithmTo recycle memory resources. Second,There are two types of garbage in the memory: one is to call the destructor of the object, and the other is not to call.GCThe collection of the former requires two steps. The first step is to call the object's destructor, and the second step is to recycle the memory, but note that these two steps are not performed in the GCTwo rounds are required for one round robin. Compared with the latter, the memory is only recycled.
So for program resources, what should we do and how to do it, in order to make the program more efficient, while occupying resources can be released as soon as possible. As mentioned above, there are two types of resources: managed memory resources, which do not require us to worry about. The system has already managed for us. For unmanaged resources, here we reiterate that stream, database connection, related objects of GDI +, and COM objects need to be manually released.

How should we release these operations .. NET provides three methods, which are also the most common, roughly as follows:
<! -- [If! Supportlists] --> 1. <! -- [Endif] --> destructor;
<! -- [If! Supportlists] --> 2. <! -- [Endif] --> inherits the idisposable interface and implements the dispose method;
<! -- [If! Supportlists] --> 3. <! -- [Endif] --> provides the close method.

after the previous introduction, we can know that the Destructor can only be called by GC , so we cannot determine when it will be called, therefore, it is not reasonable to use it as a resource release because the resource release is not timely. However, to prevent resource leakage, it will be called by GC , therefore, destructor can be used as a remedy. The difference between close and dispose is that, after the close method of the object is called, the object may be used again. For the dispose method, the resources occupied by this object need to be marked as useless, that is, the object is destroyed and cannot be used again. for example, for the common sqlconnection class, after the close method is called, you can re-open the database connection through open, if you do not need this object, you can call the dispose method to mark it as useless and wait for GC collection. After understanding the meaning of the two methods, do not distort the meaning of the two methods when you add interfaces to your own classes. GC uses the concept of generation to improve the efficiency of collection. The principle is that the object created before the first collection belongs to generation 0, and then, every time this generation number is recycled, it will be moved backwards. That is to say, the original generation 0 is changed to generation 1 during the second recycling, the objects created after the first recycle and before the second recycle will belong to generation 0. GC will first try to recycle the objects belonging to generation 0, because these are the latest, so it is most likely to be recycled, for example, some local variables in a function are not referenced (can be recycled) When exiting the function ). If enough memory is recycled in generation 0, GC will not be recycled again. If not, GC will try to be recycled in genera.

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.