Managed and unmanaged in. Net

Source: Internet
Author: User

Managed code

Managed code is visual Basic. The code compiled by the net and C # compilers. The compiler compiles the code into intermediate language (IL) instead of the machine code that runs directly on your computer. The intermediate language is encapsulated in a file called an assembly (assembly) that contains all the metadata that describes the classes, methods, and properties that you create, such as security requirements. You can copy this assembly to another server to deploy it. Typically, the action of this copy is the only action in the deployment process.

Managed code runs in the Common Language Runtime (CLR) . This runtime provides a variety of services to your running code , and in general, he loads and validates the assembly to ensure the correctness of the intermediate language . When some methods are called, the runtime compiles the specific method into a machine code that is suitable for the local computer to run, and then caches the compiled mechanical code for the next call (this is the instant compilation). As the assembly runs, the runtime continuously provides services such as security, memory management, thread management, and so on. This program is "hosted" in the runtime. Visual Basic. NET and C # can only produce managed code. If you write a program in such a language, the resulting code is managed code. If you prefer, Visual C + +. NET can generate managed code. When you create a project, select the name to be. Managed the project type that starts with. For example. Managed C + + application.

unmanaged code

Unmanaged code is code that was created before Visual Studio. NET 2002 was released, such as Visual Basic 6, Visual C + + 6. Worst of all, the code generated by a stale C compiler that still has more than 15 years of history remaining on your hard drive is unmanaged code. Unmanaged code is compiled directly into the machine code of the target computer , which can only be run on the computer that compiles them, or on a computer with the same processor or almost the same processor. Unmanaged code cannot enjoy some of the services provided by the runtime , such as security and memory management. If unmanaged code requires services such as memory management, it must explicitly invoke the interface of the operating system, which typically invokes the APIs provided by the Windows SDK . In recent cases, unmanaged programs get operating system services through COM interfaces . Unlike other programming languages in the Visual Studio platform, Visual C + + can create unmanaged programs. When you create a project and select the type of project whose name begins with Mfc,atl or WIN32, the project produces an unmanaged program.

In summary, unmanaged code is code that runs outside the Common language Runtime Environment (CLR) and is executed directly by the operating system . Unmanaged code must provide its own garbage collection, type checking, security support, and other services, unlike managed code , which obtains these services from the common language runtime .

Difference between the two

For Visual Basic and C #, life is simple, because you have no other choice. When you declare a class in those languages, instances of this class are created in the managed heap, and the garbage collector (GC) helps us manage the recycling of those objects. But 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 are unmanaged types.

unmanaged type:
Class foo{   private:      int x;   Public:      foo (): X (0) {}      Foo (int xx): x (XX) {}};
Managed types
_GC class bar{   private:      int x;   Public:      Bar (): X (0) {}      Bar (int xx): x (XX) {}};

Their only difference is that the _GC keyword is in the definition of class bar. This keyword can make a huge difference to your code.

Managed types can be reclaimed by the garbage collector. They have to be created with the keyword new and will never appear in the stack. So the following line of code is legal:

Foo f;//Unmanaged type

But this line of code is illegal:

The Bar b;//managed type must be created with new

If I create a Foo object in the heap, then I have to be responsible for cleaning up this object:

foo* PF = new Foo (2);//... delete pf;//manual cleanup

The C + + compiler actually uses two heaps, a managed heap, and an unmanaged heap, and then allocates different memory by overloading the new operator to create instances of different types of classes. If I create a bar instance inside the heap, I can ignore it. When no other code is using it, the garbage collector automatically cleans up the class and frees up the resources it consumes. There are constraints on managed types: they cannot implement multiple inheritance, or are inherited versus unmanaged types; they cannot use the Friend keyword for private access, and they cannot implement a copy constructor. So, you might not want to declare your class as a managed type. But that doesn't mean you don't want your code to become managed code. In Visual C + +, you can choose.

The execution process of managed code
    1. Select compiler: To gain the benefits of the common language runtime, you must use one or more language compilers for the runtime, such as Visual Basic, C #, Visual C + +, JScript, or many third-party compilers such as Eiffel, Perl, or COBOL compiler). Because the runtime is a multi-lingual execution environment, it supports a variety of data types and language features. The language compiler that you use first determines the available runtime features, and then uses these features to design your code. The compiler (not the runtime) establishes the syntax that the code must use. If your component must be fully capable of being used by components written in other languages, your component's export type must expose only the language features that are included in the Common Language Specification (CLS).
    2. Compile, translate the source code into Microsoft intermediate Language (MSIL), and generate the required metadata.
    3. When executed, the just-in-time (JIT) compiler translates the MSIL into native code. During this compilation, the code must pass the validation process, which examines the MSIL and metadata to see if the code can be identified as type safe.
    4. Run code: The common language runtime provides a structure that enables execution to occur and the various services that can be used during execution.

Managed and unmanaged in. Net

Related Article

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.