The difference between a managed program and an unmanaged program

Source: Internet
Author: User

Describe the difference between a managed program and an unmanaged program

This article focuses on the concepts of managed and unmanaged programs, and the difference between them. Hope to help you, to see together.

AD: "Offline activities" three Xin Rui HTML 5 Enterprise aggregation 51cto-Mobile front-end technology

managed code is a Microsoft intermediate language, and his primary role is to compile the source code before the CLR execution code of the. NET framework, which means that managed code acts as a translator. The following describes managed and unmanaged code.

What is 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. This assembly is. NET world of one-stop shopping (translator note: The Assembly has a self-descriptive) deployment unit. You can copy this assembly to another server to deploy it--typically, the copy action 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 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.

What is unmanaged code?

Unmanaged code is the code that was created before Visual Studio. NET 2002 was released. For example, Visual Basic 6, Visual C + + 6, and worst of all, the code that is generated by a stale C compiler that remains on your hard drive and has more than 15 years of history is unmanaged code. Managed 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.

This can cause some confusion: when you create a managed C + + program, you build an intermediate language assembly and an executable file with an. exe extension. When you create an MFC program, it is built as an executable file of Windows native code, and the extension of this file is also an. exe. The internal structure of the two files is completely different. You can use the intermediate language Disassembler (ILDASM) to view metadata for the internal and Intermediate languages of the Assembly. If you attempt to view an unmanaged executable with an intermediate language disassembler, the disassembler will tell you that the executable does not contain a valid CLR header, so it cannot be deserialized. As can be seen, these two files have the same extension, but they are completely different.

What is the native code?

The phrase native code can be used in two different contexts. Many people think of native code as the same as unmanaged code: code built with older tools, deliberately using Visual C + + and making it run directly on the computer, and not hosted in the runtime. This can be a complete program, or a COM component, or a DLL file that can be called by managed code using COM Intero or platform invoke (PInvoke), COM Intero or platform invoke (PInvoke) Two powerful tools that can help you to reuse old code while migrating to a new technology platform.

I prefer to say unmanaged code, because this emphasizes the code that cannot take advantage of the services provided by the runtime. For example, in managed code, code access security services can prevent code that is loaded on another server from running specific operations. If your code is running unmanaged code, you can't take advantage of such a protection service.

Another meaning of native code is to describe the output of the instant compiler, the mechanical code that actually runs in the runtime. These codes are managed code, but not intermediate languages, but mechanical codes. So don't simply assume that native is equivalent to unmanaged.

Managed code means managed data?

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.

This is the unmanaged type:

    1. Class Foo
    2. {
    3. Private
    4. int x;
    5. Public
    6. Foo (): X (0) {}
    7. Foo (int xx): x (XX) {}
    8. };

This is the managed type

    1. __gc class Bar
    2. {
    3. Private
    4. int x;
    5. Public
    6. Bar (): X (0) {}
    7. Bar (int xx): x (XX) {}
    8. };

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:

    1. Foo F;

But this line of code is illegal:

    1. Bar b;

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

    1. foo* PF = new Foo (2);
    2. // . . .
    3. Delete PF;

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.

Managed and unmanaged resources, which are things in C #, are not discussed here.

Performance comparison of managed and unmanaged code

Basically everybody knows is that all. NET language will be compiled into an intermediate language called IL Assembler. But how the computer executes this intermediate code, is a lot of people do not know, even understand the wrong.

JIT is one of the most important parts of a. NET program, and the full name is the instant compiler. I just said the misunderstanding, is a lot of people (definitely not a few, asked a lot of C + + programmers, 10 have 9 this idea) that the JIT is actually similar to the Java VM, is a interpreter, read the IL assembly code at run time, It then simulates the x86 code (also known as a virtual machine). But in fact,. NET uses a more advanced technology. NET program is loaded into memory, when a certain section of IL code is run for the first time, the JIT compiler will compile the IL code, all the cost code, and then execute. That's why. NET program starts very slowly for the first time!

With the. NET Library, Microsoft also comes with a tool that can be used beforehand. NET program all IL code compiled cost code and stored in the cache, so that the program is the same as C + + compiled, no difference, the runtime can also be out of the JIT (here do not confuse, here is not to say can be detached. NET Library, but it does not need to be in the process of instant compilation). So, please do not put. NET and Java, two of the running efficiency is not a level at all!

The optimization of the JIT means that it can be optimized for the local CPU at compile time. Traditional programs compile with the most common set of instructions (such as the old 386 instruction set) to ensure compatibility. And the JIT knows the specific type of CPU, can make full use of these additional instruction set to compile, this performance improvement is very considerable.

The difference between a managed program and an unmanaged program

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.