References
About hosting and non-hosting
What is hosting and what is unmanaged research?
Preface
I recently read ASP. net MVC 4 Framework secrets, including many Microsoft. net, many of which mentioned hosting and non-hosting, which made me confused in the cloud. Today I am taking the time to sort it out. Most of the content is based on others.
Managed Code
ManagedCodeIs the Code Compiled by the Visual Basic. NET and C # compilers. The compiler compiles the code into an intermediate language (IL) instead of a machine code that can run directly on your computer. The intermediate language is encapsulated inProgramThe Assembly file contains all metadata describing the classes, methods, and attributes you have created (such as security requirements. You can copy this Assembly to another server to deploy it. Generally, this copy operation is the only operation in the deployment process.
ManagedCodeInCommon Language Runtime Library (CLR). This Runtime Library provides various services for your running code. Generally, it loads and verifies the Assembly to ensure the correctness of the intermediate language. When some methods are called, The Runtime Library compiles the specific methods into a mechanical code suitable for running on the local computer, and then caches the compiled mechanical code for future calls.(This is instant compilation).As the Assembly runs, the Runtime Library continuously provides various services, such as security, memory management, and thread management. This program is "hosted" in the Runtime Library. Visual Basic. NET and C # Can only generate hosted code. If you write programs in this type of language, the generated code is managed code. If you want to, Visual C ++. Net can generate managed code. When creating a project, select a project whose name starts with. managed. For example,. Managed C ++ application.
Unmanaged code
UnmanagedCode is the Code created before Visual Studio. NET 2002 is released, such as Visual Basic 6 and Visual C ++ 6. The worst thing is that the Code generated by the old C compiler that is still alive in your hard disk and has more than 15 years of history is not managed. The unmanaged code is directly compiled intoMechanical code,These codes can only run on computers that compile and translate them, or on other computers with the same processor or almost the same processor.. Unmanaged code cannot enjoy services provided by some runtime libraries, such as security and memory management. If the unmanaged code requires memory management and other services, it must explicitly call the interfaces of the operating system. Generally, they call the APIS provided by the Windows SDK for implementation. In recent cases, unmanaged programs use the COM interface to obtain operating system services. And Other Visual Studio platformsProgramming LanguageDifferent, Visual C ++ can create unmanaged programs. When you create a project and select a project whose name starts with MFC, ATL, or Win32, the project will generate an unmanaged program.
All in all, the unmanaged code runs inCommon Language Runtime Environment (CLR)Code that is directly executed by the operating system. The unmanaged code must provide its own services such as garbage collection, type check, and security support.Managed codeDifferent, the latter gets these services from the public Language Runtime Library.
Differences
For Visual Basic and C #, life is simple, because you have no other options. When you declare a class in those languages, the instance of this class will be created in the managed heap, And the Garbage Collector (GC) will help us manage the collection of these objects. However, in Visual C ++, you have another option. Even if you are creating a managed program, you can decide which classes are managed types and which classes areUnmanagedType.
Unmanaged Type:
ClassFoo {Private:IntX;Public: Foo (): X (0) {} Foo (IntXX): X (XX ){}};
Managed type
_ GCClassBar {Private:IntX;Public: Bar (): X (0) {} Bar (IntXX): X (XX ){}};
The only difference between them is that the bar class has the _ GC keyword defined. This keyword will bring a huge difference to the code.
The managed type can be recycled by the garbage collector. They must be created with the keyword "new" and will never appear in the stack.So the following lineCodeIt is legal:
Foo F;//Unmanaged type
However, this line of code is invalid:
Bar B;//The managed type must be created using new.
If I create a foo object in the heap, I must be responsible for clearing this object:
Foo * pF =NewFoo (2);//...Delete PF;//Manual cleanup
The C ++ compiler actually uses two heaps, one hosting heap and one unmanaged heap, and then creates instances of different types of classes by reloading the new operator, allocate different memory.If I create a bar instance in the heap, I can ignore it. When no other code is using the class, the garbage collector automatically cleans up the class and releases the resources it occupies. There are some constraints on managed types: they cannot implement multi-inheritance or non-managed types; they cannot implement private access using the friend keyword, and they cannot implement copy constructors. Therefore, you may not want to declare your class as a managed type. But that doesn't mean you don't want your code to be hosted. In Visual C ++, you can select.
Managed Code Execution Process
- Select the compiler:To obtain the advantages of the Common Language Runtime Library, one or more language compilers must be used for the Runtime Library, such as Visual Basic, C #, Visual C ++, JScript, or one of many third-party compilers (such as Eiffel, Perl, or COBOL compiler. Because the runtime is a multi-language execution environment, it supports various data types and language functions. The language compiler you use first determines the available Runtime library functions, and then uses these functions to design code. The syntax that must be used by the compiler (instead of the Runtime Library) to create code. If your component must be fully usable by components written in other languages, the export type of your component must only disclose the language functions included in the public language specification (CLS.
- Compile, SetSource codeTranslate to Microsoft intermediate language (msil) and generate the required metadata.
- During execution, the real-time (JIT) compiler translates msil into local code. During this compilation process, the Code must pass the verification process, which checks the msil and metadata to see if the code can be identified as type-safe.
- Run code: the Common Language Runtime Library provides the structure of various services that can be used during execution.