C # managed and unmanaged Resources

Source: Internet
Author: User

In A. NET programming environment, the system's resources are divided into managed and unmanaged resources.

Managed Resources:NET platform, the CLR provides programmers with a good memory management mechanism that allows programmers to write code without explicitly releasing the memory resources they use (which in previous C and C + + requires programmers to explicitly release themselves). This management mechanism is called GC (garbage Collection). The role of GC is obvious, when the system memory resources are scarce, it will be fired, and then automatically release the unmanaged resources that are not used (that is, the programmer did not explicitly release the object). so hosting is the. NET Framework is responsible for managing your memory and resource releases, without your own control, and of course, objects are targeted only at managed resources. like int,string,float,datetime and the like, you certainly haven't seen them needing close (), because the GC will automatically free up the memory they occupy;
unmanaged resources: you need to explicitly release system resources (memory), and so on. such as file stream, database connection, System window handle, printer resource, etc. After you have read the file, you need to close and so on for each stream. For example, when SqlDataReader reads the data, it needs reader.close (); the GC can only track the lifetime of an unmanaged resource without knowing how to release it. This will cause Windows to run slower when resources are exhausted and services are not available. For example, when you link to the database, you do not explicitly release the database resources, if you continue to apply for database resources, the program will throw an exception at some point.
So, when we encapsulate operations on unmanaged resources in classes, we need to explicitly or implicitly dispose of those resources. The finalize and Dispose methods are respectively used in implicit and explicit operations.
finalize is typically used for classes with a base class that does not have a close method or an explicit method without a dispose, that is, in the finalize process we need to implicitly implement the release of the unmanaged resource, and then after the finalize process is complete, the system Own to release the managed resources.

C # managed and unmanaged Resources

Related Article

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.