Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is managed or unmanaged code in programming?

People also ask

What is managed code and unmanaged code with example?

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.

What is an unmanaged code?

What is Unmanaged code? A code which is directly executed by the operating system is known as Unmanaged code. It always aimed for the processor architecture and depends upon computer architecture.

What is the meaning of managed code?

To put it very simply, managed code is just that: code whose execution is managed by a runtime. In this case, the runtime in question is called the Common Language Runtime or CLR, regardless of the implementation (for example, Mono, . NET Framework, or . NET Core/.

Is C++ unmanaged code?

So, by that definition all code compiled by traditional C/C++ compilers is 'unmanaged code'. Also, since it compiles to machine code and not an intermediate language it is non-portable.


This is a good article about the subject.

To summarize,

  1. 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.

An application program that is executed within a runtime engine installed in the same machine. The application cannot run without it. The runtime environment provides the general library of software routines that the program uses and typically performs memory management. It may also provide just-in-time (JIT) conversion from source code to executable code or from an intermediate language to executable code. Java, Visual Basic and .NET's Common Language Runtime (CLR) are examples of runtime engines. (Read more)

  1. 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.

An executable program that runs by itself. Launched from the operating system, the program calls upon and uses the software routines in the operating system, but does not require another software system to be used. Assembly language programs that have been assembled into machine language and C/C++ programs compiled into machine language for a particular platform are examples of unmanaged code.(Read more)

  1. Native code is often synonymous with Unmanaged, but is not identical.

Here is some text from MSDN about unmanaged code.

Some library code needs to call into unmanaged code (for example, native code APIs, such as Win32). Because this means going outside the security perimeter for managed code, due caution is required.

Here is some other complimentary explication about Managed code:

  • Code that is executed by the CLR.
  • Code that targets the common language runtime, the foundation of the .NET Framework, is known as managed code.
  • Managed code supplies the metadata necessary for the CLR to provide services such as memory management, cross-language integration, code access security, and automatic lifetime control of objects. All code based on IL executes as managed code.
  • Code that executes under the CLI execution environment.

For your problem:

I think it's because NUnit execute your code for UnitTesting and might have some part of it that is unmanaged. But I am not sure about it, so do not take this for gold. I am sure someone will be able to give you more information about it. Hope it helps!


When you think of unmanaged, think machine-specific, machine-level code. Like x86 assembly language. Unmanaged (native) code is compiled and linked to run directly on the processor it was designed for, excluding all the OS stuff for the moment. It's not portable, but it is fast. Very simple, stripped down code.

Managed code is everything from Java to old Interpretive BASIC, or anything that runs under .NET. Managed code typically is compiled to an intermediate level P-Code or byte code set of instructions. These are not machine-specific instructions, although they look similar to assembly language. Managed code insulates the program from the machine it's running on, and creates a secure boundary in which all memory is allocated indirectly, and generally speaking, you don't have direct access to machine resources like ports, memory address space, the stack, etc. The idea is to run in a more secure environment.

To convert from a managed variable, say, to an unmanaged one, you have to get to the actual object itself. It's probably wrapped or boxed in some additional packaging. UNmanaged variables (like an 'int', say) - on a 32 bit machine - takes exactly 4 bytes. There is no overhead or additional packaging. The process of going from managed to unmanaged code - and back again - is called "marshaling". It allows your programs to cross the boundary.


In as few words as possible:

  • managed code = .NET programs
  • unmanaged code = "normal" programs

Managed code is what C#.Net, VB.Net, F#.Net etc compilers create. It runs on the CLR, which among other things offers services like garbage collection, and reference checking, and much more. So think of it as, my code is managed by the CLR.

On the other hand, unmanaged code compiles straight to machine code. It doesn't manage by CLR.