C # hosting and non-hosting

Source: Internet
Author: User

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

The recovery of managed resources requires no manual intervention, and you cannot intervene in the recovery. What can be done?

Only understand how. Net CLR performs these operations. That is to say, for most objects created by your application, you can rely on. NET

The framework Garbage Collector implicitly executes all necessary memory management tasks.

For unmanaged resources, you must release these resources after they are used in the application. For example

A file object of system. Io. streamreader. The close () method of the call object must be displayed to close it. Otherwise, it will occupy the system

Memory and resources, and unexpected errors may occur.

Here, I want to know what is managed resources and what is non-managed resources?

The most common type of unmanaged resources is the objects that encapsulate operating system resources, such as files, windows, or network connections.

Although the garbage collector can track the lifetime of objects that encapsulate unmanaged resources, it does not know how to clear these resources. Fortunately. net

Framework provides the finalize () method, which allows the Garbage Collector to clear unmanaged resources when it recycles such resources. If

Searching finalize in the msdn library will find many similar topics. Here we list several common unmanaged resources:

Applicationcontext, brush, component, componentdesigner, container, context, cursor, filestream, Fon

T, icon, image, matrix, object, odbcdatareader, oledbdatareader, pen, RegEx, socket, streamwriter, time

R, tooltip, and other resources. It is possible that many of them did not notice it during use!

About managed resources, we don't need to scatter it, such as simple int, String, float, datetime, etc. Over 80% of. net resources are all hosted.

Manage resources.

The. NET Framework provides the object. Finalize method, which allows the object to recycle

Such as the memory used to properly clean up its unmanaged resources. By default, the Finalize method does not perform any operations. By default,

The Finalize method does not perform any operations. If you want the Garbage Collector to clear the object before it recycles the object's memory

The Finalize method must be rewritten in the class. However, you can find that the override method finalize is not available in actual programming.

() In C #, you can use the destructor to automatically generate the Finalize method and call the Finalize method of the base class.

For example:
~ Myclass ()
{
// Perform some cleanup operations here.
}
This code is implicitly translated into the following code.
Protected override void finalize ()
{
Try
{
// Perform some cleanup operations here.
}
Finally
{
Base. Finalize ();
}
}

However, in programming, the override method finalize () is not recommended, because the implementation of the Finalize method or destructor performs

It may have negative effects. A simple reason is as follows: using the Finalize method to reclaim the memory used by objects requires at least two garbage collections.

When the Garbage Collector recycles, it only recycles inaccessible memory without the Finalize method. In this case, it cannot Recycle

Memory that cannot be accessed by the Finalize method. It removes the items of these objects from the termination queue and places them in the tag

In the Object List for "prepare to terminate", the items in this list point to the object in the managed heap that is preparing to be called to terminate the code, the next garbage collection

The memory is recycled and released.

C # hosting and non-hosting

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.