The key is to understand the CLR (Common language Runtime)
The. Net Framework is made up of two separate and interrelated parts: The CLR and the class library, the CLR is the service it provides to us, and the class library is the function it implements. . NET most of the features----garbage collection, versioning, thread management, etc., all using the services provided by the CLR
When you compile the source code for the. NET framework, the resulting target code is not a machine instruction that the CPU can recognize, but rather a new language called "Microsoft Intermediate Language (MSIL, or code abbreviated as IL)". The CLR provides a real-time compiler to compile IL code into native machine code. This allows the CLR to make the code portable because. NET application's source code must be compiled into IL code that can run on any platform that provides CLR services. From the CLR's perspective, all languages are equal, as long as there is a compiler that can generate IL code, which ensures interoperability across languages.
Code that writes and uses the CLR service for the CLR is called "managed code," and code that is not using the CLR service (that is, code you've been writing for years) is called "Unmanaged code."
Discusses the application of C + + in a. NET managed environment, because C + + is not. NET platform design, so Microsoft needs to make some extensions to C + +, which introduces the concept of "Managed Extensions", which allows us to use the. NET Framework in C + + projects, and you must use Managed Extensions if you have the following development needs:
1. Porting existing C + + code to a Managed environment
2. Accessing the. NET Framework class in C + + code
3. Adoption. NET language access to existing C + + code
Managed code is a similar code to a Java virtual machine, but unlike a Java virtual machine, its execution is very efficient, it is called IL code, or MSIL code, that is, the meaning of the intermediate code
Unmanaged code is the meaning of local code, which means that it is sent directly to the CPU at execution time.
com/com++ components, ActiveX controls, API functions, pointer operations, self-made resource files ... These unmanaged, others are managed.
The recycling of managed resources does not require human intervention to recycle, and you cannot interfere with their recycling, all you can do is understand how the. NET CLR does these things. This means that for most objects created by your application, you can rely on the. NET Framework's garbage collector to perform all necessary memory management tasks implicitly.
For unmanaged resources, after you have used these unmanaged resources in your application, you must display a file object that frees them, such as System.IO.StreamReader, and must display the close () method of the calling object to close it, otherwise it will consume the system's memory and resources. And there may be unexpected errors.
I would like to say here, it must be clear what is a managed resource, what is unmanaged resources?
The most common type of unmanaged resource is an object that wraps an operating system resource, such as a file, window, or network connection, although the garbage collector can track the lifetime of an object that encapsulates an unmanaged resource, but it does not know how to clean up those resources. Fortunately, the. NET Framework provides the Finalize () method, which allows appropriate cleanup of unmanaged resources when the garbage collector reclaims that class of resources. If you search for Finalize in the MSDN Library, you will find a number of similar topics, listing several common unmanaged resources: ApplicationContext,Brush,component,componentdesigner, Container,context,cursor,filestream,font,icon,image,matrix,object,odbcdatareader,oledbdatareader
, Pen,regex,socket,streamwriter,timer,tooltip and so on resources. May be in use when a lot of did not notice!
With regard to managed resources, there's no mention of Satan, like simple int,string,float,datetime, and so on, more than 80% of the resources in. NET are managed resources.
How unmanaged resources are freed, the. NET Framework provides a object.finalize method that allows an object to properly clean up its unmanaged resources when the garbage collector reclaims the memory used by the object. By default, the Finalize method performs no action. By default, the Finalize method performs no action. If you want the garbage collector to perform cleanup operations on objects before reclaiming the memory of the object, you must override the Finalize method in the class. However, you can see that in real programming you cannot override the method Finalize (), and in C #, you can automatically generate a Finalize method and a call to the base class's Finalize method through a destructor.
For example:
~myclass ()
{
Perform some cleanup operations here.
}
The code is implicitly translated to 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 implementing a Finalize method or destructor can have a negative impact on performance. A simple reason for this is that the memory used to reclaim objects using the Finalize method requires at least two garbage collection, and when the garbage collector reclaims it, it reclaims only inaccessible memory without the finalizer (Finalize method), and he cannot recycle a finalizer (Finalize method) Memory that cannot be accessed. Instead, it removes the items from the terminating queue and places them in the list of objects marked "ready to terminate", which points to the object in the managed heap where its termination code is ready to be called, and the memory is reclaimed and freed the next time the garbage collector recycles.
Excerpt from: Http://wenku.baidu.com/link?url=LGTBo9u4d0QOC38bJTO5WEjK_WIR_ Ahlxunxyscdgkkysatfuewam5qn7ev7ax2s67iec8b1jqlttukep-dembgekirxrciis8fz09bwuxm
. NET managed code and unmanaged code