Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

managed code vs unmanaged code

I'm trying to get my mind wrapped around the concept of managed vs unmanaged code. Correct me if I'm wrong but managed code is anything that gets compiled down to bytecode while unmanaged code gets compiled down to machine code.

Is this correct?

like image 659
conterio Avatar asked Jul 15 '16 20:07

conterio


People also ask

What is the difference between managed and unmanaged code?

Managed Code is converted to IL, Intermiddiate Language also termed as CIL of MSIL. Unmanaged Code is converted to native language code. Programmer has no low level access using Managed Code. Programmer can write low level access code using unmanaged code.

Is C# managed or unmanaged?

It also provides memory allocation, type safety, etc to the code. The application is written in the languages like Java, C#, VB.Net, etc. are always aimed at runtime environment services to manage the execution and the code written in these types of languages are known as managed code.

What is the managed and unmanaged code in net?

It gets the managed code and compiles it into machine code. After that, the code is executed. The runtime here i.e. CLR provides automatic memory management, type safety, etc. C/C++ code, called "unmanaged code” do not have that privilege.


1 Answers

From annakata's answer:

Managed code is not compiled to machine code but to an intermediate language which is interpreted and executed by some service on a machine and is therefore operating within a (hopefully!) secure framework which handles dangerous things like memory and threads for you. In modern usage this frequently means .NET but does not have to.

Unmanaged code is compiled to machine code and therefore executed by the OS directly. It therefore has the ability to do damaging/powerful things Managed code does not. This is how everything used to work, so typically it's associated with old stuff like .dlls

Now, what's going on under the hood? Managed vs unmanaged is all about memory.

In managed code, the code itself doesn't directly manipulate the memory. It gives instructions to a runtime that does it on the code's behalf. That way, unsafe or illegal operations can be blocked, and the code operates in a semi-sandbox. Managed languages can and often do have unmanaged code - such is the nature of computing.

Visualize memory like a giant parking lot. The difference between a managed and unmanaged language is like this:

In a managed language, you walk up to the valet and explain that you want 10 cars parked. You don't know exactly where they're parked, but you know that the valet will gladly retrieve them for you any time you want. You also don't get to decide where they're parked - you just get to tell the valet that they need to be parked.

In an unmanaged language, it's your job to pick the spots. You could drive into other cars, park in handicapped spots, whatever- this gives you better performance (you don't have to constantly relay instructions to a valet) but you can also make a lot of stupid/dangerous mistakes.

like image 186
Athena Avatar answered Sep 21 '22 18:09

Athena