The confusion of C + + to C #: destructors and related __jquery

Source: Internet
Author: User
Turn from: http://blog.csdn.net/zhuweisky/article/details/415665 the confusion of C + + to C #: destructors and related

 

Programmers moving from C + + to C # are often confused by destructors, Dispose methods, close methods, and Finalize methods in C #, and it's even more unintelligible to see a finalize queue, a freachable queue.

Yes, C + + there is not so much trouble in the Dongdong, there is only one destructor in C + +, usually this destructor does nothing, but if you have requested or assigned resources in the constructor or other member functions of the class that the destructor belongs to, you must remember to release the resources in the destructor. Otherwise it will cause a resource leak--This is one of the biggest pitfalls in C + + programs. The main purpose of the destructor in C + + is described above, and there are many other uses, such as descending the count in the destructor, or declaring the destructor as private to restrict inheritance and limit the generation of objects in the stack that belong to the class that the destructor belongs to, and so on.

And look at the c#,c#. So many concepts related to destructors, what is the difference between them. Why do you need these things that are easy to confuse? And listen to me slowly.

one. Methods related to Destructors

As with C + +, destructors in C # are primarily used to release the requested resource, but it only needs to release the unmanaged resources, because the managed resource is handled by the garbage collector GC. Note that the GC is ignorant of unmanaged resources such as window handles, open files, and streams.

1. Destructors and Finalize methods

In C + +, destructors are marked by the "~ Class name", which can also be done in C #, such as

Class A

{

~a ()

{

Console.WriteLine ("A ' s destructor");

}

}

In C #, however, destructors are extended by the compiler to the overridden System.Object Finalize method, which is equivalent to a destructor alias, except that the definition or invocation of the Finalize method is not allowed directly in your code. Such as

Class A

{

protected override void Finalize ()//error.

{

Console.WriteLine ("A ' s destructor");

}

}

In this way, we can think of destructors and finalize methods as the same thing, except that the destructor is explicitly written, and the Finalize method is obtained by the compiler transformation of the destructor, which is implicit. If not for a clearer understanding of the garbage collection process, our programmers may not need to know the existence of the Finalize method. If however one. NET programmer does not understand garbage collection technology, I believe he will not be a good one. NET programmer. Let's mention here that if a class declares a destructor, it will be automatically called asynchronously before an instance of that class is purged by the GC. Of course, if a class doesn't declare a destructor, it does not produce a corresponding Finalize method, then when the reference count of one instance of the class changes to 0 o'clock, it is cleared in the next round of garbage collection without having to wait until the next round of garbage collection. Therefore, an instance of a class that has no destructor is more efficient than an instance of the corresponding class that has destructors when it is recycled. A further description of this will appear in the following article.

2. Dispose (Close) method

The Dispose (Close) method and the destructor function are the same, and their code is almost identical. And the Dispose method and the Close method are only aliases for each other.

However, the close (Dispose) method is called by the user, and if a destructor is defined in a class other than the closed (dispose) method, the Gc.suppressfinalize () should be called at the end of the closing (Dispose) method. method to tell the GC that it does not need to invoke the Finalize method when it clears object memory. When the close (Dispose) method is called on an object, the object still exists until the GC reclaims it, and the programmer cannot directly and precisely control when the managed object dies.

 

3. The difference between a Finalize method (destructor) and the Dispose method

The most important difference is that the Finalize method (that is, the destructor) is invoked automatically by the GC, and the Dispose method is called directly by the programmer.

Supposedly, as long as the Finalize method is enough, why do you have a Dispose method to stir the water. The answer is that a programmer calls the Dispose method to release the various unmanaged resources that the object holds at the right time. Because we cannot control when GC destroys objects that we no longer use, we cannot control when the GC invokes the Finalize method that we no longer use the object, and we need to release the important resources held by the object in a timely manner, and then we can do so by invoking the Dispose method.

The Finalize method acts as a protective measure to clean up unmanaged resources when the Dispose (close) method is not invoked.

 

two. Finalization queues and freachable queues

when it comes to finalize queues and freachable queues, you have to raise garbage collection techniques.

. NET in the garbage collection technology is based on the Mark sweep algorithm. The mark Sweep algorithm is divided into mark (marked "Live" object) and Sweep (clear) two stages, and in order to reduce fragmentation, also joined the compact phase. For a more detailed description of garbage collection, see the article "Understanding. NET CLR Garbage Collection Technology" in the October 2003 "Development guru".

1. Finalization queues

In the case of a managed heap allocation object (that is, new), if the GC finds that the object implements a Finalize method, it adds it to the finalization queue (that is, add a reference or pointer to it in the finalization queue).

Suppose class exc implements the Finalize method (C #)

EXC D = new exc ();

After executing the above statement, the finalization queue is shown in Figure 1.

2. freachable queues

When the managed heap is low on memory, the GC starts to recycle the heap. When the GC detects that an object's reference count is 0, then G checks if there is a pointer to the object in the finalization queue, and if so, puts it into the freachable queue (that is, adding a reference or pointer to it in the freachable queue), otherwise Clears the object directly and releases its memory.

Figure 1 Finalization Queue example

In Figure 1, we know that object A will be put into the freachable queue, and Object C's memory will be purged directly by the GC. As shown in the following figure

Figure 2 Freachable Queue Example

The Finalize method of an object in the Freachable queue is executed by a special thread. This thread is normally inactive, and at some point it wakes up and, when it finds that the freachable queue is not empty, one by one executes the Finalize method of the object in this queue. After execution the object is the same as a garbage object without a finalize method, that is, after the destructor of the A object in Figure 2 is executed, no references are pointed to it in the finalization queue, as shown in the following figure

Figure 3 the finalization queue after the Finalize method of a object is executed

Now a object, like a garbage object without a Finalize method, will be purged directly at the next round of GC startup.

After reading the above introduction, I believe you have a basic understanding of the concepts of destructors, Dispose methods, close methods and Finalize methods in C #, finalize queues, freachable queues, and their roles. Of course, in describing these concepts, in order to describe the goal more clearly, I ignore a lot of related things, such as the generation of garbage collection technology, as well as strong references, weak references, and so on, if you need to further understand the relevant content, you can access the relevant information, the information on these is very rich.

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.