C # Learning Notes---Dispose (), Finalize (), SuppressFinalize

Source: Internet
Author: User

Http://www.cnblogs.com/eddyshn/archive/2009/08/19/1549961.html

There are actually two functions for freeing resources in. NET objects:Dispose and Finalize. The purpose of finalize is to release unmanaged Resources , while Dispose is used to free all resources, both managed and unmanaged.

In this pattern, the void Dispose (bool disposing) function uses a disposing parameter to distinguish whether the current is being called by Dispose (). If it is called by Dispose (), both managed and unmanaged resources need to be released at the same time. If it is called by ~foo () (that is, the Finalize () of C #), then only the unmanaged resources need to be freed.

This is because the Dispose () function is explicitly called by other code and requires that the resource be freed, and finalize is called by the GC. Other managed objects referenced by Foo when the GC is called may not need to be destroyed, and will be called by the GC even if it is to be destroyed. So only unmanaged resources can be released in finalize. On the other hand, because managed and unmanaged resources have been freed in Dispose (), it is not necessary to call finalize again when the object is reclaimed by GC, so call Gc.suppressfinalize (this) in Dispose () Avoid repeating the call to finalize.

However, even if finalize and dispose are repeatedly called, there is no problem, because the existence of variable m_disposed, the resources will only be freed once, the redundant calls will be ignored.

As a result, the above pattern guarantees:

1. Finalize releases only unmanaged resources;

2. Dispose releases both managed and unmanaged resources;

3. It is no problem to call finalize and dispose repeatedly;

4. Finalize and Dispose share the same resource release policy, so there is no conflict between them.

In C #, this pattern needs to be implemented explicitly, where C # 's ~foo () function represents finalize (). In C + +/CLI, this pattern is auto-implemented, and the class destructor is not the same.

According to C + + semantics, destructors are called when they go out of scope or delete. In Managed C + + (that is, managed C + + in. NET 1.1), the destructor is equivalent to the Finalize () method in the CLR, which is called by the GC at garbage collection time, so the timing of the call is ambiguous. In. NET 2.0 C + +/CLI, the semantics of the destructor are modified to be equivalent to the Dispose () method, which implies two things:

1. All CLR classes in C + +/CLI Implement Interface IDisposable, so you can use the Using keyword to access an instance of this class.

2. Destructors are no longer equivalent to finalize ().

----------------------------others to summarize------------------------

1.The Finalize method (in C # is a destructor, the following destructor) is used to free unmanaged resources, and managed resources are automatically reclaimed by the GC。 So, we can also differentiate between managed and unmanaged resources. All of the resources that will be automatically reclaimed by GC are managed resources, and resources that cannot be automatically reclaimed by GC are unmanaged resources. The use of unmanaged resources directly in our classes is rare, so we do not have to write destructors basically.

2, most of the unmanaged resources will have a lot of negative impact on the system, such as the database connection is not released may cause the connection pool to run out of available database connections. A file that does not close causes other processes to not read and write to the file.

Implementation model:
1. Because most unmanaged resources require manual release, we should expose a method specifically for releasing unmanaged resources. The Dispose method that implements the IDispose interface is the best model because C # supports the using statement fast and can automatically call the Dispose method when it leaves the statement block.

2, although you can manually release unmanaged resources, we still want to release the unmanaged resources in the destructor, so that is a secure application. Otherwise, if you forget to manually release unmanaged resources because of a programmer's negligence, it can have disastrous consequences. So releasing an unmanaged resource in a destructor is a remedial measure, at least for most classes.

3, because the invocation of the destructor will cause the GC to reduce the efficiency of the object collection, so if you have completed the destruction of the destructor of the thing (such as releasing unmanaged resources),you should use the SuppressFinalize method to tell the GC that no more destructors for an object need to be executed.

4. Destructors can only release unmanaged resources and cannot operate on any managed objects/resources. Because you can't predict the timing of the destructor, you might have freed the managed resources you were working on when the destructor was executed. This will result in serious consequences.

5. (This is a rule) if a class has a member that implements the IDispose interface type and creates (note that it is created, not received, it must be created by the class itself) its instance object, then this class should also implement the IDispose interface, And in the Dispose method, all the dispose methods that implement the members of the IDispose interface are called.
This is the only way to ensure that all objects that implement the IDispose interface's object's Dispose method can be called to ensure that any resources that need to be freed can be released manually.

C # Learning Notes---Dispose (), Finalize (), SuppressFinalize

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.