Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm missing something basic here. What's the use of compiling from a source language into bytecode (java) or intermediate language (.NET) and then running them from inside the JVM or CLR ?


There's the decrease in performance (however slight or large) of using managed code, but what are the benefits ? I know there's garbage collection and memory management but even so wouldn't it be better to just compile the source to native straight away without needing this intermediate level ?

Also (I'm adding this here since it's directly related to the question) - Apparently Windows 10 Universal apps are compiled with .NET Native which compiles to native machine code. I'm curious as to why this wasn't done before with all .NET programs.

like image 635
Aniruddha Varma Avatar asked Oct 16 '15 06:10

Aniruddha Varma


2 Answers

Apart from everything else pointed out in the other answers, major benefits of this approach are the important cost reductions achieved in development and maintenance and the vastly improved scalability of the development environment.

Consider the case where there is no intermediate language; you would need to develop and mantain a compiler for each supported language and each supported platform. Say you have languages L1, L2 and L3 and platforms P1, P2 and P3. This would mean you'd need to develop and mantain 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 lets you develop 3 language specific compilers C1(L1, I), C2(L2, I) and C3(L3, I) and 3 platform specific compilers C4(I, P1), C5(I, P2) and C6(I, P3).

Evidently, the larger your supported language and platform base is, the more signifcant the cost reduction will be.

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

It's basically a win win situation.

like image 116
InBetween Avatar answered Oct 27 '22 16:10

InBetween


This is from MSDN:

Benefits of managed code

Managed languages provide a generalized way to handle the details of memory management and garbage collection, at the cost of a small amount of overhead. This trade-off frees you from error-prone tasks, and allows you to write more compact, readable, and error-free programs.

Benefits of Un-managed code

If you use an unmanaged language, such as C++, you must write extra code to manage memory and security, and clean up objects after they have served their purpose. The housekeeping details are complicated, and don't relate to the intended function of the program, so developers often neglect these tasks, ignore them, or lose track of them. As a result, unmanaged code is often more costly and time consuming to test, and it requires greater programmer training and discipline.

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

like image 20
S.Dav Avatar answered Oct 27 '22 17:10

S.Dav