Use C # to implement the standard Dispose mode

Source: Internet
Author: User
This article describes how to compile your own resource management code when creating types of resources that do not contain memory, especially when dealing with non-memory resources.
We already know the importance of processing objects that occupy uncontrolled resources. Now we should write resource management code to handle types that contain non-memory resources. The entire. NET Framework component uses a standard mode to process non-memory resources. Users of the type you set up also want you to follow this standard mode. The idea of the standard processing mode is as follows: when the client remembers it, use the IDisposable interface to release your uncontrolled resources, and use finalizer in a protective manner when the client forgets it ). It works with the Garbage Collector to ensure that the object is affected by the performance related to the Terminator only when necessary. This is a good way to deal with uncontrolled resources, so we should fully understand it.
The root base class in the class hierarchy must implement the IDisposable interface to release resources. You must add a Terminator for this type as the defense mechanism. All these programs delegate the release of resources to a virtual method. Derived classes can reload this method according to their resource management needs. As long as the derived class must release its own resources and it must call the base class version of the function, it must reload this virtual method.
At the beginning, if your class uses non-memory resources, it must contain a Terminator. You cannot rely on the client to always call the Dispose () method. Because when they forget to do so, you are faced with resource leaks. They do not call Dispose, but you are faulty. The only way to ensure that non-memory resources are correctly released is to establish a Terminator.
When the garbage collector is running, it immediately deletes all junk objects without a Terminator from the memory. All objects with Terminator still exist in the memory. These objects are added to the end queue. The garbage collector raises a new thread and periodically runs the Terminator on these objects. After these Terminator threads finish their work, they can delete junk objects from the memory. Objects to be terminated stay in the memory for a longer time than objects without a Terminator. But you have no choice. To protect programs, you must write a Terminator when the type contains uncontrolled resources. However, you do not have to worry about performance issues. The next step ensures that the client avoids termination-related performance overhead.

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.