The note of learning ASP. NET [19] About GC, object destruction, and using

Source: Internet
Author: User
In the past two days, I have read the GC (memory collection mechanism) related content used to reclaim useless objects and release their occupied memory space... A teaching video was read five times to understand what he meant. It's hard for me to understand English. After reading it, I learned what kind of object is within the GC recycle range, and learned that GC only recycles the memory when the memory is insufficient. Next we should look at the process and relationship between object destruction and GC. When GC recycles an object, it calls a Finalize method in the object. Although we may not write this method in our class, it does exist. Because all our classes inherit from the system. object class, And the Finalize method is a protected method of the object class. We never call that method manually, because the method is automatically called by GC only when the GC destroys the object. In most cases, we do not care about this method. GC will automatically process objects and release resources such as memory in a timely manner. So when will we need to pay attention to this Finalize method? The answer is: when we call. net runtime environment management resources, such as opening files and data streams in the system, the most typical is that our object opens a database connection during runtime, this database connection resource will not be automatically released because our objects are destroyed. Therefore, we need to set the code to release the database connection in the Finalize method, in this way, even if we do not manually close the database connection during encoding, GC will release the database connection resources when the object is recycled. However, system. the Finalize method of the object is protected and cannot be reloaded. If we need to do the work before the custom object is destroyed, we need to use it. NET provides the Destructor for us. The Destructor is the same as the class name and must be added before the class name ~. For example, the destructor of the person class should be ~ Person () {// execute the code for processing such as releasing resources here} said this may find that in this example, if we open the database connection in the object and it is not closed, even if we provide the destructor to release the database resource connection, the database connection will not be closed immediately, GC recycles useless objects and executes their destructor only when the memory is insufficient. For resources that need to be released immediately after use, we need to implement the idisposable interface for our class and implement the dispose method in the interface. We put the code for releasing database connection resources or other resources not managed by. net into the dispose method. It seems that the dispose method is the same as the destructor, but they are different. The Destructor are called by GC, while the dispose method is called by ourselves. In this way, we can immediately execute the dispose method after using this object to release non-. Net managed resources (such as database connections ). The following is a class that implements the idisposable interface and dispose method:
Using system;
Using system. Collections. Generic;
Using system. Text; namespace objectsandclasses
{
Class disposedemo: idisposable
{
Private system. Io. filestream FS = NULL; // The alreadydisposed variable is used to monitor whether our object has executed the dispose method.
Private bool alreadydisposed = false; Public void dispose ()
{
If (! This. alreadydisposed)
{
If (FS! = NULL)
FS. Close ();
Console. writeline ("in dispose ");
}
This. alreadydisposed = true;
// The following code indicates that GC does not need to execute the destructor of the object when destroying the object, because we have already done the work that the Destructor needs to do in the dispose // method. In this way, GC // improves the efficiency of object collection.
GC. suppressfinalize (this );
}
}
} The following is an example of calling the class written above: Private Static void testdispose ()
{
// Declare the variable:
Disposedemo demo = NULL; try
{
// Assign the variable a value:
Demo = new disposedemo ();
}
Finally
{
If (Demo! = NULL)
{
Demo. Dispose ();
}
}
} The above method may be a little difficult to use. In fact, we can also use using. The Code is as follows: Private Static void testusing ()
{// The following Code creates an object and assigns a value to the reference variable demo. // It is only contained in the brackets behind using. After using is used, demo // The domain of this object is limited to the braces after using. When the code // in braces reaches the end, the dispose method of the demo object is automatically called to release the resource. In fact, this code serves the same purpose as the previous one, but it is easier to use. Note that only classes that implement the idisposable interface can be operated by // using.
Using (disposedemo demo = new disposedemo ())
{
// Do something with demo in here...
}
}

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.