6

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?

conterio
  • 1,087
  • 2
  • 12
  • 25
  • It is all about garbage collection, what makes managed code distinctive is that the GC can find back object references that the code uses. The just-in-time compiler plays a very crucial role in this scheme, beyond compiling MSIL to machine code (just like a C compiler does with C code), it *also* generates a table that tells the GC where to look for object references. That table is the difference, unmanaged code can't provide that table. – Hans Passant Jul 15 '16 at 22:16
  • Possible duplicate of [What is managed/unmanaged code in C#?](https://stackoverflow.com/questions/334326/what-is-managed-unmanaged-code-in-c) – T.Todua Nov 29 '18 at 21:55

1 Answers1

7

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.

Athena
  • 3,200
  • 3
  • 27
  • 35
  • Can one not have a managed language that compiles to machine code? I thought the distinction was strictly about having a garbage collector or not. Does one imply the other? – JamesFaix Jul 15 '16 at 20:53
  • If it compiles to machine code, then the only way to add features like a garbage collector would be to emit an _enormous_ amount of assembly into the code during compilation. That is to say, the proverbial 'valet' would have to be inserted in just about every other line of code as it was converted to assembly. – Athena Jul 15 '16 at 20:55
  • What about old-school LISP reduction machines? (Not that you *should* know the answer to it, or that it really matters these days.) – JamesFaix Jul 15 '16 at 20:58
  • I have no idea, to be honest. The analogy is powerful enough as an explanation in nearly all cases. Even if that was what was happening, the fundamental behavior is not significantly changed. – Athena Jul 15 '16 at 20:59
  • Your answer to the OP was great, sorry it I was getting tangential. – JamesFaix Jul 15 '16 at 21:02
  • @JamesFaix I did some research, you may be right. It seems to depend heavily on the language. Sometimes it happens at runtime (Python, Java) sometimes it happens after compilation, but before execution. At some point, everything has to make its way to native code though./ – Athena Jul 15 '16 at 21:03
  • Thanks, this was what i was looking for! – conterio Jul 15 '16 at 22:14