Garbage collection GC in C #

Source: Internet
Author: User

Used in the beginner stage. net has never considered the garbage Resource Recovery rate during programming. It is because the teacher is always talking about nothing in the classroom and does not care about it, from then on, I wrote a program without fear! The program is stuck, the memory is soaring, and occasionally several memory errors occur. When you see this, it is too big. Now let's think about it. The class teacher only heard the first half of the sentence...

Chatting is nothing, and you don't have to worry about what you are doing to prevent leaks of what secrets, what technologies, and so on. (The following is my opinion, if there are similarities, Please bypass ,,,)

How to collect garbage in. Net:

Run. NET applications, the object instances created by the program will be tracked by the CLR, And the CLR records which objects will be used (there is a reference relationship ); which objects will not be used (no reference relationship exists ). CLR will sort out the objects that will not be used again. At the right time, it will destroy some objects according to certain rules and release the memory occupied by these objects.

Speaking of this, a new technical point is introduced:

How does CLR record the object reference relationship?

CLR makes object relationships into a "tree Chart" to mark their reference relationships.

How does CLR release the object's memory?

The key technology is: CLR transfers useless objects together to make the memory continuous, and the newly allocated objects are created on this continuous memory, so as to reduce memory fragments. Note! CLR does not move large objects

What rules does the Garbage Collector collect junk objects?

CLR collects Objects Based on the lifetime of the objects in the memory. The shortest time is allocated to 0th generations, and the longest is allocated to 2nd generations. There are three generations in total.

Generally, 0th of loan objects are small objects, 2nd of generation objects are large objects, and 0th of generation objects have the shortest GC collection time (in milliseconds ), the GC collection time of the 2nd generation object is the longest. When the program requires memory (or when the program is idle), GC will first collect The 0th generation objects,

After the collection, it is found that the released memory is still insufficient, and GC will collect the 1st-generation and 2nd-generation objects. (Usually collected in this order)

If GC runs and the memory space is insufficient, an OutOfMemoryException is thrown.

After GC runs several times, The 0th generation object still exists, so the CLR will move these objects to the 1st generation, and the 1st generation object will also be like this.

Now that the garbage collector is available, why do we need the Dispose method and destructor?

Due to CLR, GC can only release managed resources, but cannot release unmanaged resources (database links, file streams, etc ).

How can we release unmanaged resources?

Generally, we choose to implement the IDispose interface for the class and write a Dispose method.

Ask the caller to manually call the Dispose method of this class (or use the using statement block to automatically call the Dispose method)

During Dispose execution, neither the Destructor nor the Garbage Collector began to process the release of this object.

Sometimes, we don't want to implement the Dispose method for a type, and we want it to automatically release unmanaged resources. You need to use the destructor.

The Destructor is a very strange function. The caller cannot call the object's destructor. The Destructor are called by GC.

You cannot predict when the Destructor will be called, so try not to operate on the managed resources that may be recycled here. The Destructor is only used to release unmanaged resources.

GC releases the object containing the destructor, Which is troublesome (it takes two times to kill her ),

CLR will first execute the Destructor and then collect the memory it occupies.

Do we need to manually perform garbage collection? In what scenarios?

When does GC execute garbage collection is a very complicated algorithm (Policy)

It can be roughly described as follows:

If GC finds that many objects are collected last time and a large memory is released,

Then it will perform the second recovery as soon as possible,

If it is frequently recycled, but not much memory is released,

It will slow down the recovery frequency.

Therefore, do not call GC. Collect () as much as possible, which will damage the existing GC execution policy.

Unless you are familiar with the memory usage of your application and know when a large amount of garbage will be generated, You can manually intervene in the work of the garbage collector.

I have a large object. I am worried that GC will collect it for a long time,

 

What is the relationship between weak references and garbage collection?

When a large object is used and there is no reference relationship, GC will automatically recycle the memory occupied by it.

When GC recycles an object large enough, it may take a little longer. When the user needs to use the object again, we can extract the object from the recycle pool again, weak references are involved here. The Code is as follows:

Var bss = new BsCtl (BrowserContainer); var vbss = new WeakReference
 
  
(Bss); bss = null; BsCtl OK; vbss. tryGetTarget (out OK); // if no garbage collection is performed, OK will not be NULL if (OK = null) {// if garbage collection has been performed, this code will be executed OK = new BsCtl (BrowserContainer );}
 


Garbage collection can collect bss objects at any time,

If the data is collected, it enters the if statement block. if the data is not collected, it does not enter the if statement block. TryGetTarget (out OK) successfully fetches the bss from the garbage collection.

Note: Only short and weak references are mentioned here, and long and weak references are not mentioned. I think there are few scenarios for long and weak references.

Advantages of the garbage collector:

Because I do not have a lot of C/C ++ programming experience, if you want to talk about the advantages of the garbage collector, it is necessary to compare it with a lower-level language such as C/C ++. Therefore, the general answer is to reduce the BUG of improper memory usage and improve programming efficiency.




Related Article

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.