Why do we need / use managed code (over native)?

Something is missing for me here. How to use compilation from source language to bytecode (java) or intermediate language (.NET), and then run them from JVM or CLR?


There is a decrease in performance (no matter how little or little) from using managed code, but what are the advantages? I know garbage collection and memory management, but isn’t it even better to just compile the source code right now without requiring this intermediate layer?

Also (I add this here, as it is directly related to the question). Apparently, Windows 10 Universal applications are compiled with .NET Native , which compiles to native machine code. I am curious why this has not been done before with all .NET programs.

+6
source share
5 answers

In addition to everything else noted in other answers, the main advantages of this approach are the important cost reductions achieved during development and maintenance, as well as significantly improved scalability of the development environment.

Consider the case where there is no intermediate language; You will need to develop and use a compiler for each supported language and each supported platform. Let's say you have the languages ​​L1, L2, and L3 and the platforms P1, P2, and P3. This would mean that you would need to develop and use 9 different compilers: C1 (L1, P1), C2 (L1, P2), C3 (L1, P3), C4 (L2, P1), etc.

On the other hand, having an intermediate common language, I allow you to develop 3 compilers of the language C1 (L1, I), C2 (L2, I) and C3 (L3, I) and 3 for the platform C4 (I, P1), C5 (I , P2) and C6 (I, P3).

Obviously, the larger your supported language and platform base, the more significant the cost reduction will be.

It also gives you great flexibility in future additions to supported platforms or languages; any new L4 language will require the development of only one C (L4, I) compiler, and you immediately support all platforms for the price of one development. Conversely, if you have a new P4 platform, you only need to develop C (I, P4) and, bingo, you have L1, L2 and L3, all work on P4.

This is basically a winning situation.

+2
source

This is from MSDN :

Benefits of Managed Code

Managed languages ​​provide a generic way to handle memory management and garbage collection details with a small amount of overhead. This compromise frees you from problematic tasks and allows you to write more compact, readable, and error-free programs.

Benefits of Unmanaged Code

If you use an unmanaged language such as C ++, you must write additional code to manage memory and security and clean up the objects after they have completed their task. Household details are complex and not related to the intended function of the program, so developers often neglect these tasks, ignore them or lose sight of them. As a result, unmanaged code is often more expensive and time consuming to test, and this requires more training and discipline for programmers.

However, developers often prefer unmanaged code because it runs faster, provides more flexibility in the use of pointers, and gives direct control of the hardware.

+2
source

This is because compiling into native code will make your software platform specified. However, when compiling as an intermediate language, your program is portable and can run on each platform if there is a specific JVM / CLR platform.

Both Java and .Net use just-in-time compilation so that they can provide portability, but still better performance than using the interpreter.

In addition, Microsoft has already provided Ngen with Visual Studio to compile .Net code into native code:
https://msdn.microsoft.com/en-us/library/6t9t5wcf(v=vs.110).aspx
The main difference between Ngen and .Net Native is that Ngen is still dependent on the .Net Framework and .Net Native compiles the required .Net code with the program, so installing the .NET Framework is not required.

0
source

Bytecode and the JVM "shenanigans" mean that the code is always interpreted the same way, regardless of the platform on which it is running. I remember that I vaguely read many years ago that Intel and AMD (as well as other processors) perform some logical operations in different ways, which leads to different results on these processors and can lead to some cross-platform errors. Therefore, the bytecode solves this, and the result is always constant.

On the other hand, if you know that you only program on one platform and want to get extra performance, then this is where embedded software development (or low-level languages) is embedded. Java and .NET are best used when you know performance is not so important.

-1
source

In java, when bytecode is generated that is specific to a particular type of architecture, java helps its ability to β€œcompile ever.” In addition to this, it takes up very little memory.

-1
source

All Articles