Some Understanding of. Net garbage collection C # programming (Finalize and Dispose (bool disposing) and Dispose ()

Source: Internet
Author: User

The similarities between Finalize and Dispose (bool disposing) and Dispose () are as follows:

These three services are designed to release unmanaged resource services.

Differences between Finalize and Dispose () and Dispose (bool disposing:

  1. Finalize is a mechanism provided by CRL. It ensures that if a class implements the Finalize method, the garbage collector calls the Finalize method when the class object is recycled. developers of this type must release unmanaged Resources in the Finalize method. however, when will Finalize be called is determined by the garbage collector, and the user (customer) of this type of object cannot control it. therefore, it is impossible to release valuable unmanaged resources in a timely manner. because unmanaged resources are more valuable, this will reduce performance.
  2. Dispose (bool disposing) is not a mechanism provided by CRL, but a design pattern (as a method of the IDisposable interface). It aims to make it available to users (customers) of class objects) after using the class object, you can manually call the release of the unmanaged resource in time, without waiting for the time when the class object is reclaimed. developers of such classes only need to port the code originally written in Finalize to the Dispose (bool disposing) to release unmanaged resources. in Finalize, you only need to simply call "Dispose (false)" (Why is false passed.

At this time, we may be confused. Why do we still need a Dispose () method? Is it possible to have only one Dispose (bool disposing) or only one Dispose?
The answer is:
There is only one Dispose (). Why? Because if there is only one Dispose () and there is no Dispose (bool disposing) method. in the code for handling the release of unmanaged resources, you cannot determine whether the method is called by the customer or by the garbage collector through Finalize. it cannot be determined that if it is manually called by the customer, the garbage collector does not want to call Finalize () (call GC. supperFinalize method ). another possible cause (we know that if the garbage collector is called through Finalize, we may reference other managed objects in the release code, at this time, these managed objects may have been garbage collected, which will lead to unpredictable execution results (do not reference other Managed Objects in Finalize ).

Therefore, a bool disposing parameter is required, but if there is only one Dispose (bool disposing), there is a funny requirement for the customer that Dispose (false) has been used by Finalize, the customer must use the Dispose (true) method, but who can ensure that the customer will not use the Dispose (false) method? So here we adopt a design pattern: Reload implements Dispose (bool disposing) as protected, while Dispose () is implemented as Public, so that the customer can only call Dispose () (The Internal call of Dispose (true) // indicates that it is a direct call by the customer), and the customer cannot call Dispose (bool disposing ).

Example:

Public class BaseResource: IDisposable
{
// The Destructor automatically generates the Finalize method and calls the Finalize method of the base class. by default, a class has no destructor, that is, the Finalize method is not called when the object is reclaimed by garbage collection.
~ BaseResource ()
{
// Do not write code that releases unmanaged resources here to ensure code readability and maintainability
// It must be called in the Dispose (false) method, and false indicates that the Dispose (bool disposing) function is called from the garbage collector when the Finalize is called.
Dispose (false );
}


// Cannot be called directly by the customer
// If disposing is true, this method is called directly by the customer, and the hosted and unmanaged resources can be released.
// If disposing is false, the function is called from the garbage collector when calling Finalize. In this case, other hosted objects cannot be referenced. Therefore, only unmanaged resources can be released.
Protected virtual void Dispose (bool disposing)
{

// This method is directly called by the customer, so the hosted and unmanaged resources can be released.
If (disposing)
{
// Release managed resources
OtherManagedObject. Dispose ();
}


// Release unmanaged Resources
DoUnManagedObjectDispose ();


// This method is directly called by the customer, telling the Garbage Collector to clear itself from the Finalization queue to prevent the garbage collector from calling the Finalize method.
If (disposing)
GC. SuppressFinalize (this );

}

// Can be called directly by the customer
Public void Dispose ()
{
// It must be called in the form of Dispose (true). If it is true, the Dispose (bool disposing) function is directly called by the customer.
Dispose (true );
}
}

The purpose of the above example:

1/If the customer does not call Dispose () and fails to release the managed and unmanaged resources in time, there is still a chance to execute Finalize () to release the unmanaged resources during garbage collection, however, this results in a waste of idle resources that are not released in a timely manner.

2/If the customer calls Dispose (), the managed and unmanaged resources can be released in time. When the object is garbage collected, Finalize () is not executed (), improves the efficiency of using unmanaged resources and improves system performance.

You can refer to the use experience of New, Open, and Close (Internal call Dispose () of the SqlConnection object to learn more about them. Thank you!

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.