In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what is the difference between managed and unmanaged programs on the Internet". The content is easy to understand and clear, hoping to help you solve your doubts. Let the editor lead you to study and learn the article "what is the difference between managed and unmanaged programs on the Internet".
Managed code is an intermediate language of Microsoft, and its main role is to compile the source code before the CLR execution of the .NET FRAMEWORK, that is, managed code acts as a translator.
What is managed code?
Managed code is code compiled by Visual Basic .NET and C # compilers. The compiler compiles the code into an intermediate language (IL) instead of machine code that can be run directly on your computer. The intermediate language is encapsulated in a file called assembly, which contains all the metadata that describes the classes, methods, and properties you create, such as security requirements. This assembly is an one-stop shopping deployment unit in the .NET world. You can copy this assembly to another server to deploy it-typically, the action of this copy is an operation 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, it loads and validates assemblies to ensure the correctness of the intermediate language. When certain methods are called, the runtime compiles the specific method into mechanical code suitable for the local computer to run, and then caches the compiled mechanical code for the next call. This is just-in-time compilation.
As the assembly runs, the runtime continues to provide 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 generate managed code. If you write programs in this language, the resulting code is managed code. Visual C++ .NET can generate managed code if you like. When you create a project, select a project type whose name starts with .Managed. For example, .managed C++ application.
What is unmanaged code?
Unmanaged code is code that was created before the release of Visual Studio .NET 2002. For example, Visual Basic 6, Visual C++ 6, and worst of all, even the code generated by old C compilers that are still on your hard drive and are more than 15 years old is unmanaged code. Managed code is compiled directly into the mechanical code of the target computer, which can only be run on the computer that compiled them, or on other computers 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 needs services such as memory management, it must explicitly call the operating system's interface, which typically calls the API provided by Windows SDK. In recent times, unmanaged programs get operating system services through the COM interface.
Unlike other programming languages on the Visual Studio platform, Visual C++ can create unmanaged programs. When you create a project and choose a project type whose name starts with MFC,ATL or Win32, the project produces an unmanaged program.
This can lead to some confusion: when you create a managed C++ program, you build an intermediate language assembly and an executable with a .exe extension. When you create a MFC program, you build an executable file with native Windows code, which also has a .exe extension. The internal structure of the two files is completely different. You can use the intermediate language disassembler (ildasm) to view the internal and intermediate language metadata of the assembly. If you try to use an intermediate language disassembler to view an unmanaged executable, the disassembler will tell you that the executable does not contain a legitimate CLR header, so it cannot be decompiled. It can be seen that although the two files have the same extension, they are completely different.
What is native code?
The phrase native code can be used in two different contexts. Many people will think of native code as the same meaning as unmanaged code: code built with older tools that deliberately adopts Visual C++ and runs directly on the computer, and is not managed 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 call (PInvoke), COM Intero or platform call (PInvoke) can help you migrate to a new technology platform and still reuse old code.
I prefer unmanaged code because it emphasizes code that cannot take advantage of the services provided by the runtime. For example, in managed code, the code access security service prevents code loaded on another server from running specific operations. If your code is running unmanaged code, you can't take advantage of such protection services.
Another meaning of native code is to describe the output of the just-in-time compiler, the mechanical code that is actually running in the runtime. This code is managed code, but it is not an intermediate language, but mechanical code. So don't simply assume that native is synonymous with 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 that class will be created in the managed heap, and the GC will help us manage the collection of these objects. But in Visual C++, you have another choice. Even if you are creating a managed program, you can decide which classes are managed and which are unmanaged.
This is the unmanaged type:
Class Foo {private: int x; public: Foo (): X (0) {} Foo (int xx): X (xx) {}}
This is the managed type.
_ _ gc class Bar {private: int x; public: Bar (): X (0) {} Bar (int xx): X (xx) {}}
The difference between them * is that the _ _ gc keyword is included in the definition of class Bar. This keyword can make a big difference to the code.
Managed types can be recycled by the garbage collector. They must be created with the keyword new and will never appear on the stack. So this line of code is legal:
Foo f
But this line of code is illegal:
Bar b
If I create a Foo object in the heap, I must be responsible for cleaning up the object:
Foo* pf = new Foo (2) / /. . . Delete pf
The C++ compiler actually uses two heaps, a managed heap and an unmanaged heap, and then allocates different memory for creating instances of different types of classes by overloading the new operator.
If I create an instance of Bar in the heap, I can ignore it. When no other code is using it, the garbage collector automatically cleans up the class, releasing the resources it consumes.
There are some constraints on managed types: they cannot implement multiple inheritance, or inherit and unmanaged types; they cannot use the friend keyword for private access, 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 managed code. In Visual C++, you have a choice.
Managed and unmanaged resources, which are in C#, are not discussed here.
Performance comparison between managed code and unmanaged code
Basically everyone knows that all .net languages will be compiled into an intermediate language called IL assembly. But how the computer executes this intermediate code is something that many people do not know or even misunderstand.
JIT is one of the most important parts of. Net program running, and its full name is just-in-time compiler. The misunderstanding I just said is that many people (definitely not a few, asked a lot of C++ programmers, and 9 out of 10 have this idea) think that JIT is actually something similar to Java VM, an Interpreter that reads IL assembly code at run time and simulates it into x86 code (commonly known as virtual machines). But in fact, .NET uses more advanced technology. After the .net program is loaded into memory, when a piece of IL code is run * times, the JIT compiler will compile all the IL code into native code and then execute it. This is why the .NET program starts slowly every time it runs!
Along with the. NET library, Microsoft also comes with a tool that compiles all the IL code of a .NET program into native code in advance and saves it in the cache, so that the program is exactly the same as that compiled by C++, without any difference, and can be run without JIT (not to be confused here, not to break away from the. NET library, but to say that there is no need for real-time compilation). So, please don't confuse .NET and Java, the efficiency of the two is not the same level at all!
JIT optimization means that it can be optimized at compile time for the local CPU. When traditional programs are compiled, in order to ensure compatibility, they usually use the most general instruction set (such as the ancient 386 instruction set) to compile. JIT knows the specific types of CPU and can make full use of these additional instruction sets for compilation, which is a significant performance improvement.
These are all the contents of this article entitled "what is the difference between managed programs and unmanaged programs on the Internet". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.