Some Understanding of C # programming related aspects (finalize and dispose (bool disposing) and dispose () of. Net garbage collection [zz]

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.