Use the idisposable interface to release. NET Resources

Source: Internet
Author: User
You can use the dispose mode to release resources appropriately, but it increases system overhead.
By Mickey Williams

You can use the dispose mode to release non-memory resources, such as database connections, Win32 InterOP components, and operating system handles. Do not expect the Garbage Collector to immediately release the resource because it is triggered when the memory of the managed heap is insufficient. You can quickly consume a small amount of resources, such as database connections,ProgramScalability, resulting in a secondary impact. The dispose mode cannot be implemented unless necessary because it may increase system overhead, which can be avoided in many cases.

In. net, the dispose mode is implemented by an idisposable interface, which includes a simple method -- dispose:

Interface idisposable {void dispose ();}

The most obvious example is that an instance of the class must implement idisposable, such as a local data connection or an operating system handle, to seize an unmanaged resource.

In addition, remember the following example that should be ignored to implement the idisposable interface. When a class implements idisposable, the correct usage of the instance is to call the dispose method to delete the object when it is not needed. Therefore, when you implement a class, when this class contains other classes that implement idisposable, you must call the dispose method. This usually means that you must implement idisposable in this class, even if it cannot directly process uncontrolled resources.

The following is a typical mode for implementing the idisposable interface:

Public class slalomracer: idisposable {bool _ disposed = false; Public bool isdisposed {get {return _ disposed;} set {_ disposed = value ;}}~ Slalomracer () {internaldispose (false);} public void dispose () {internaldispose (true);} protected void internaldispose (bool disposing) {If (disposing) {GC. suppressfinalize (this); _ managedthing. dispose ();} _ unmanagedthing. dispose ();} [...]}

PreviousCodeWhen idisposable is implemented, you can call the disposal code in two ways. First, if you call the dispose method directly, all the controlled and non-controlled objects will be listed as targets to be cleared. We can see that the termination operation will execute an optimization step to prevent objects from being cleared. You can also safely call the dispose method multiple times. After the dispose method is called, a flag is used to ensure that no method on this object can be called. The sample code is as follows:

Public void seekhottub () {If (isdisposed) throw new objectdisposedexception ("BT ");}

Objectdisposedexception will remind you that you have used a disposed object. It is necessary to trigger an exception when calling other methods on a disposed object -- after all, you cannot use these disposed objects again.

Second, if you do not call this dispose method, the terminate operation will call dispose (false) by itself, and it will adopt a slightly different code path from the previous code. First, do not clear the control objects, even if they implement the idisposable interface. You cannot determine whether the object reference is valid-these objects may be waiting for operation termination or have been terminated. Second, there is no need to call GC. suppressfinalization because these objects have been terminated.

Finally, if you are using C #, you should use its inherent support for the idisposable interface to clear objects. You can use the following statement:

Using (slalomracer Mickey = new slalomracer () {// use Mickey here Mickey. rungates (); Mickey. getstitches ();} // Mickey disposed automatically here

C # The editor sends out the Il code that calls the dispose method as appropriate, even if an exception occurs.

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.