Issues with Resource releases

Source: Internet
Author: User

Turn

Personal understanding, the destruction seems to be a little chicken, can not show the call, not to determine the timing of the call, should be just as a release of the existence of a resource mechanism

Then, the resource release of an instance class should occur at the end of the method that hosts it, so it is not possible to write some miscellaneous methods in the main function of the main program end flag, and the method should be written out of the master program, so that it can save resources and reuse when it is used. I don't know if I understand the problem. If there is something wrong, please also point out, the novice!

The difference between Dispose, destructor, and Close in C #

Guided reading: In C #, destructors and Dispose are free resources, destructors are used to implicitly dispose of resources, and Dispose is used to explicitly dispose of resources, that is, the destructor is automatically called after the object is inaccessible, and dispose is called by the class consumer; The Close method means that the resource is closed. Does not necessarily represent the release of resources.

Destructors cannot display calls, and for Dispose, the Close method, a display call is required to be executed.

One, the difference between the two methods of close and dispose

Once the Close method of the object has been called, the object may be reused, and the Dispose method will have the resources it needs to be marked as useless, that is, the object is being destroyed and can no longer be used. such as common. NET class Library SqlConnection this class, when the Close method is called, you can reopen a database connection through open, when the object is completely unused, you can call the Dispose method to mark this object useless, waiting for GC reclamation.

The difference between the two and three

Destructors

Dispose method

Close method
Significance Destroying objects Destroying objects Close Object Resource
Invocation mode The call cannot be displayed, and the GC recycle is called Need to show calls or pass using statements Need to display the call
Call timing Not sure OK, in the show call or leave the Using program block OK, when the display is tuned

Iii. description of destructors and Dispose

1. Dispose requires the implementation of the IDisposable interface.

2. Dispose is called by developer Code, and destructors are called automatically by the GC.

3. The Dispose method should free all managed and unmanaged resources. Destructors should only dispose of unmanaged resources. Because the destructor is called by the GC, when the GC determines that an object is no longer needed, the destructor is called, and the object may contain other useful managed resources.

4. It is recommended to call the Dispose method by using the system GC's frequent call to destructors to release resources to reduce system performance.

5. Add code "GC" at the end of the Dispose method. SuppressFinalize (this); ", which tells the GC that it does not need to call the destructor of the object again, or that the GC will still call its destructor after judging that the object is no longer useful, although the program does not go wrong, but it affects system performance.

6. The destructor and dispose should release the same resources, so that the resource will be freed in Finalize even if the class consumer does not call Dispose.

7. Finalize should not be public.

8. When a Dispose method exists, it should be called because Finalize frees the resource is usually very slow.

Iv. description of the Close function

The Close method has different meanings in different classes, and there is no requirement that close has a special meaning, that is, close does not necessarily release resources, and you can let the Close method say "close".

However, because close has a "close" meaning, it is also permissible to release the resources by close. For example, in a file operation, releasing an object with Close seems more accurate than the Dispose meaning, so when you design the class, you can set Close to public, dispose to protected, and then call Dispose by close.

That is to say what the Close means, it will not release the resources, completely determined by the Class Designer. It is a one-sided way to say "Close calls Dispose" on the web. In SqlConnection, close simply means closing the database connection and not releasing the SqlConnection object resource.

Based on experience, when close and Dispose are present at the same time (both public), close does not mean freeing resources because, typically, class designers should not use the two public methods to free up the same resources.

Five, destructor and Dispose method instances

C # codeCopy
{~baseresource (){//in order to maintain the readability and maintainability of the code, do not write code to release unmanaged resources here//must be called in Dispose (false),   False tells the Dispose (bool disposing) function to be the Dispose (false) call from the garbage collector when the destructor is called; }//cannot be called directly by the client//If disposing is true, then this method is called directly by the client, then the managed, and unmanaged resources can be freed//if disposing is false, then the function is called from the garbage collector in the fin Alize, you should not refer to other managed objects at this time. Therefore, you can only dispose of unmanaged resources protected virtual void Dispose (bool disposing) {//Then this method is called directly by the client, then both managed, and unmanaged resources can be released if (disposing) {//Release      Hosting Resources Othermanagedobject.dispose ();   }//Releasing unmanaged Resources dounmanagedobjectdispose ();      Then this method is called directly by the customer, telling the garbage collector to purge itself from the finalization queue, thereby preventing the garbage collector from invoking the Destructor method. if (disposing) GC.   SuppressFinalize (this); }//Can be called directly by the client public void Dispose () {//must be called in Dispose (true), telling di as true   The Spose (bool disposing) function is Dispose (true) that is called directly by the client; } }

Problem with resource release

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.