C # understanding of garbage collection finalize and Dispose

Source: Internet
Author: User
The destructor in C # actually overwrites the virtual method in system. object. Finalize

The three most common methods are as follows:

1. destructor (called by GC, not sure when to call it)

2. inherit the idisposable interface to implement the dispose method. (You can call it manually. For example, the connection to the database, sqlconnection. Dispose (), because timely release will affect the database performance. This will be used at this time, for example, opening a file. Other operations, such as deletion, will be affected if the file is not released. After dispose is called, this object can no longer be used and will be recycled by GC .)

3. Provide the close method. (Similar to dispose, but after calling the close method, you can re-open it through open)

The Destructor cannot display the call. For the last two methods, the display call is required to be executed. The difference between the close and dispose methods 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 will be destroyed and cannot be used again. For example, common. the sqlconnection class in the. NET class library. After the close method is called, you can re-open a database connection through open. When this object is completely unnecessary, you can call the dispose method to mark this object as useless, 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.

Destructor

Dispose Method

Close Method

Meaning

Destroy object

Destroy object

Disable object Resources

Call Method

It cannot be displayed and called. It is called during GC collection.

Call to be displayed

Or use the using statement.

Call to be displayed

Call time

Uncertain

Are you sure you want to call or exit usingProgramBlock

OK.

The following provides a mode to combine the above destructor with the dispose method.

Public class baseresource: idisposable

{

// Previously we mentioned that the Destructor actually overwrites the system. the virtual method finalize in the object. By default, a class has no destructor, that is, the Finalize method is not called when the object is reclaimed by garbage collection.

~ Baseresource ()

{

// To maintainCodeReadability and maintainability. Do not write code to release unmanaged resources here.

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

Finally:

If your class uses unmanaged resources, consider providing the close method and open method. Call the close method first in your dispose method.

When a class already exists, such as sqlconnection. If you do not need this connection, you can use the close () method. If not, consider calling

Dispose () method.

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.