Memory recycling, dispose, close, finalie (destructor in C)

Source: Internet
Author: User

Net resources are managed and unmanaged. The so-called hosting refers to the resources managed in CLR (general language runtime), which can be automatically recycled by CLR. this is also known as GC (garbage collection mechanism ). for unmanaged resources, such as database connections and COM connections, you need to manually clear and Recycle resources. to clear unmanaged resources, we can use destructor to execute them. Although the execution time is unknown, they will eventually be executed. of course, there are also dispose () and close () methods. The difference between the two is that after close (), they must be opened with open (), while dispose () is completely destroyed.
--- GC is required to use destructor. collect () will be executed automatically (GC, collect () will be automatically executed in the automatic recycle mechanism, you can also explicitly call it) and dispose () needs to be explicitly called, you can also use using (). However, note that when using destructor, you can call the function in at least two steps to reclaim the memory. then, using () or *. after dispose (), GC will execute the Destructor again. therefore, add GC in dispose. supressfinalize (this) to prevent re-calling the destructor.
Conclusion: The call needs to be displayed for dispose () and close (). Dispose () can be called through using. the Destructor cannot be explicitly called. dispose () and destructor are destroy objects, while close () is close. You can open them again through open. the time for calling the Destructor is unknown, while dispose () is executed in explicit or using () mode, and close () is executed in explicit mode. all three are used to destroy unmanaged objects. A classic C #-dispose:

1 private bool _ isdisposed = false; 2 ~ Mytest () 3 {4 // This. close (); 5 dispose (false); 6} 7/*** // <summary> 8 // memory is released, which must be explicitly called by such instances, such as SQL. dispose (); or using () 9 // </Summary> 10 public void dispose () 11 {12 // idisposable dispose = this as idisposable; 13 // If (dispose! = NULL) 14 // {15 // dispose. dispose (); 16 //} 17 dispose (true); 18 GC. suppressfinalize (this); 19} 20 protected virtual void dispose (bool disposing) 21 {22 if (! _ Isdisposed) 23 {24 if (disposing) 25 {26 // release of managed resources 27} 28 // release of unmanaged resources 29 _ isdisposed = true; 30} 31}

Memory recycling, dispose, close, finalie (destructor in C)

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.