The key is to understand CLR (runtime in public languages)
. Net Framework is composed of two independent and related parts: CLR and class library. CLR is the service it provides for us. Class Library is the function it implements .. most of NET's features-garbage collection, version control, thread management, and so on, all use the services provided by CLR ..
When you are. when the NET Framework compiles the source code, the target code is not a machine instruction that the CPU can recognize, but a kind of code called "Microsoft intermediate language (MSIL, or IL for short). CLR provides a real-time compiler for compiling IL code into local machine code. in this way, the CLR can make the code portable, because. NET application source code must be compiled into IL code, which can run on any platform that provides CLR services. from the perspective of CLR, all languages are equal, as long as there is a compiler that can generate IL code, which ensures the interoperability of various languages.
The code for writing and using the CLR service for CLR is called "managed code", and the code that does not use the CLR Service (that is, the code you have been writing for many years) it is called "unmanaged code ".
Discussed in C ++ in.. NET hosting environment, because C ++ is not. therefore, Microsoft needs to expand C ++ to introduce the concept of "managed extension, it allows us to use it in C ++ projects.. NET Framework. If you have the following development needs, you must use managed extensions:
1. Porting existing C ++ code to the hosting environment
2. Access the. NET Framework class in C ++ code
3. Access the existing C ++ code through the. NET language
The managed code is similar to the code similar to the Java Virtual Machine, but unlike the Java virtual machine, the execution efficiency is very good. It is called the IL code or MSIL code, that is, the meaning of the intermediate code.
Unmanaged code refers to local code, that is, it is directly sent to the CPU during execution, COM/COM ++ components, ActiveX controls, API functions, and pointer operations, self-made resource files... these are not managed, others are managed.
For the collection of managed resources, manual intervention is not required, and you cannot intervene in the collection. All you can do is to understand how. net CLR performs these operations. That is to say, for most objects created by your application, you can use the. NET Framework Garbage Collector to implicitly execute all necessary memory management tasks.
For unmanaged resources, you must release these resources after they are used in the application, such as System. IO. A file object of StreamReader must be closed by calling the Close () method of the object to be displayed. 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. For such resources, although the garbage collector can track the lifetime of objects that encapsulate unmanaged resources, however, it does not know how to clear these resources. Fortunately, the. net Framework provides the Finalize () method, which allows you to clear unmanaged resources when the Garbage Collector recycles such resources. If you search for Finalize in the MSDN Library, you will find many similar topics. Here we list 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 other resources. It is possible that many of them did not notice it during use!
About managed resources, we don't need to talk about it, such as simple int, string, float, DateTime, etc. Over 80% of. net resources are managed resources.
How to release unmanaged resources. NET Framework provides the Object. Finalize method, which allows the Object to properly clean up its unmanaged resources when the Garbage Collector recycles the memory used by the Object. 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, you must override the Finalize method in the class. However, you can find that the override method Finalize () cannot be used 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:
[Cpp]
~ 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, the override method Finalize () is not recommended in programming, because the implementation of the Finalize method or destructor may have a negative impact on the performance. A simple reason is as follows: using the Finalize method to recycle the memory used by the object requires at least two garbage collections. When the Garbage Collector recycles, it only recycles the Finalize method) in this case, the unaccessible memory with the Finalize method cannot be recycled. Instead, it removes the items of these objects from the termination queue and places them in the list of objects marked as "prepared for termination, the items in this list point to the objects in the managed heap that are preparing to be called to terminate the code. The Garbage Collector recycles and releases the memory the next time it recycles.
From the trace