Detailed introduction to the. Net garbage collection mechanism

Source: Internet
Author: User
Destructors

Destructors cannot have modifiers, such as public. Cannot accept any parameters.

The compiler automatically converts a destructor into an override version of the Object.Finalize method, as follows.

Class test{  protected override void Finalize ()  {    try {...}    finally {base. Finalize (); }  }}

Garbage collector

. NET garbage collector will guarantee:

Each object is destroyed and its destructor is bound to be run. When a program finishes, all objects are destroyed.
L Each object is destroyed only once.
Each object is destroyed only when it is not reachable (that is, it does not exist to the object's reference).

Working mode:

1) It constructs a map containing all the reachable objects. To do this, it repeatedly follows the reference field in the object. The garbage collector constructs this map very carefully and ensures that circular references do not have infinite recursion. Any object in this map will not be considered unreachable.
2) It checks to see if any unreachable objects have a destructor that needs to be run (the process of running the destructor is called finalization). Any unreachable objects that need to be finalization are placed in a special queue. This queue is called the freachable queue.
3) It reclaims the remaining unreachable objects (that is, objects that do not require finalization). To do this, it moves the reachable object down in the heap, defragmenting the heap and releasing the memory at the top of the heap. When the garbage collector moves a reachable object, a reference to that object is also updated.
4) It then allows other threads to resume execution
5) in a separate thread, it performs a finalize operation on a non-reachable object (located in the freachable queue) that needs to be finalization.

As can be seen from the above summary, the existence of destructors will make the above process more than 2, 52 steps. So consider using a using block instead of a generic type. If you are using a class that implements the Dispose method (Close method). It is best to call this method in finally (it is necessary to check that the disposed property of the object to be Dispose is false before calling the method, and only if it is not true to dispose, which is the reason for the recommended use. The using can easily constrain the scope of the variable that is being refactored-that is, between a pair of curly braces. Or use a using block to surround the code that uses this class. The type of the object that is placed into the using block must implement the IDisposable interface.

Standard cleanup mode

Finally, give me a. NET recommended standard cleanup pattern code, sample code:

Class myclass:idisposable{  private bool disposed = false;//disposal State public   void Dispose ()// Public Dispose method (optional Implementation idisposal interface)  {    Dispose (true);    Gc. SuppressFinalize (this);  }   ~myclass ()  {    Dispose (false);  }   protected virtual void Dispose (bool disposing)  {    if (!disposed)    {      if (disposing)      {        // Dispose the managed resources.      }      Dispose the unmanaged Resources.    }    disposed = true;}  }

In the code above, we call the Dispose method from the destructor, which ensures that dispose executes. , in addition Gc.suppressfinalize (this), which prevents the compiler from performing a destructor on this object.

Thank you for reading, hope to help everyone, thank you for the support of topic.alibabacloud.com!

  • 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.