The official definition is:
Managed Code (Managed code) is intermediate language (IL) codes that run in the common language runtime (CLR). The compiler compiles the code into an intermediate language, and when the method is called, the CLR compiles the specific method into a machine code that is appropriate for the local computer to run, and caches the compiled machine code for the next call to be used. As the assembly runs, the CLR provides a variety of services: memory management, security management, thread management, garbage collection, type checking, and so on.
Unmanaged code , directly compiled into the target computer code, executed directly by the operating system, the code must provide its own garbage collection, type checking, security support and other services. For services such as memory management, it is necessary to display the interface that invokes the operating system, typically invoking the APIs provided by the Windows SDK for memory management.
When a project chooses a project type that begins with the name Mfc,atl,win32, the project produces an unmanaged program. 、、
Give some of the information collected to help understand this definition
The CLR (Common language runtime) is defined as:
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."
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.
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.
Difference:
1, managed code is an intermediate language, running on the CLR;
Unmanaged code is compiled into machine code and runs on the machine.
2, the managed code is independent of the platform and the language, can better realize the compatibility between different language platform;
Unmanaged code depends on the platform and language.
3, the managed code can enjoy the services provided by the CLR (such as security detection, garbage collection, etc.), do not need to complete these operations;
Unmanaged code needs to provide its own security detection, garbage collection and other operations.
The simple understanding is that managed and unmanaged refers to the operation of memory, managed code does not need to be recycled, and unmanaged code needs to recycle objects, such as operations on the database, Excel files, and so on.
Managed code in C # vs. unmanaged code