Difference between native and managed code?

.NetNativeUnmanagedManagedMachine Code

.Net Problem Overview


For example, when looking at the GlowCode profiler website it says:

> GlowCode 6.2 and x64 profile native, managed, and mixed C++, C#, .NET code

What do they mean?

.Net Solutions


Solution 1 - .Net

Native code is the code whose memory is not "managed", as in, memory isn't freed for you (C++' delete and C's free, for instance), no reference counting, no garbage collection. Managed code, you guessed it, is the code whose memory is free and allocated for you, garbage collection and other goodies.

Mixed code is when you have managed code that calls onto an unmanaged layer. Normally, when you have a pure unmanaged C++ DLL and you call it from .NET using P/invoke.

Solution 2 - .Net

Native code is compiled to work directly with the OS. Managed code however, is precompiled (bytecode in Java-speak) but is then processed by the Just In Time Compiler to native code at runtime. Managed code has the interesting side effect of having the potential of running on different operating systems, because the machine code is not created until the VM actually uses it. This way, you are able to run .NET apps on Windows and also run them on Linux or Mac that have the Mono runtime installed. The portability is not as clean currently as Java is (because of Microsoft's naturally closed architecture), but the concept remains.

If you are running an unmanaged app, the code has been compiled to run for the designated OS/Hardware. Any portability to another OS/instruction set is lost and must be recompiled to execute.

Solution 3 - .Net

Native code is written in the "native" machine language of the computer that it is running on and is executed directly by the processor.

Managed code is written in a special language that requires another program to run (i.e. manage) it. This other program is often called an interpreter as it interprets the special language.

C and C++ programs are native.

Java and C# (and all .NET languages for that matter) are managed.

Managed C++ is a special form of C++ that runs in the .NET interpreter.

A mixed program is a program that uses code that is both native and managed.

Solution 4 - .Net

Code that runs under the control of the common language runtime (CLR) is known as managed code. Code that does not run under the CLR is known as native code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoelView Question on Stackoverflow
Solution 1 - .NetAnzurioView Answer on Stackoverflow
Solution 2 - .NetWayne HartmanView Answer on Stackoverflow
Solution 3 - .NetonedozenbagelsView Answer on Stackoverflow
Solution 4 - .NetHanuView Answer on Stackoverflow