67

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?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Joel
  • 15,166
  • 16
  • 39
  • 31
  • see also http://stackoverflow.com/questions/3434202/what-is-the-difference-between-native-code-machine-code-and-assembly-code – Alexander Bird Jul 15 '11 at 14:41
  • 1
    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:48

4 Answers4

94

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.

Anzurio
  • 16,780
  • 3
  • 39
  • 49
  • 3
    The C++/CLI compiler allows managed code (compiled with `/clr` and output as MSIL) to allocate native objects on the native heap (no garbage collection). Also, the (desktop) .NET garbage collector doesn't use reference counting (that has problems with cycles). – Ben Voigt Jul 19 '12 at 22:33
  • 1
    I found this answer by Bing. Much better explained than MS website. – toddwz Mar 07 '16 at 20:16
  • 5
    Native *does not* mean memory management. Native means it is a Windows application or dll. .Net does not create either. It uses the same file extension as a native applications but the content of those exe's and dll's are not windows exe's and dll's. This is why you cannot use a .net "dll" with a native windows application, because its not a windows dll. – William Egge Apr 07 '17 at 15:54
  • 4
    Automatic memory management is only a small part of what makes a language managed. The most important distinction is that native code will compile to a binary executable, and managed code will compile to byte code that must be interpreted at runtime by a Just In Time compiler. https://learn.microsoft.com/en-us/dotnet/standard/managed-execution-process – louahola Aug 28 '17 at 23:23
58

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.

Wayne Hartman
  • 18,369
  • 7
  • 84
  • 116
  • Please explain why "because of Microsoft's naturally closed architecture", "the portability is not as clean currently as Java is". I do not see why Microsoft's naturally closed architecture will limit the ability of .NET to be as portable as Java. – Pacerier Jul 12 '15 at 14:40
  • 6
    @pacerier My answer to your question today would be different than the one I gave six years ago. Six years ago .NET was not open source, for example. But that announcement given fairly recently kinda changes everything in that regard, doesn't it? :) – Wayne Hartman Jul 12 '15 at 19:20
  • Also its important to point out that non-native cannot integrate with native. For example you cannot create an isapi application with .net because it does not create windows dll's, or you cannot create plugins for native applications using .net because the things .net makes are not OS files. – William Egge Apr 07 '17 at 15:59
43

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.

onedozenbagels
  • 2,242
  • 2
  • 19
  • 21
  • 3
    I just wanted to point out that there is a difference between the concept of an interpreted language, such as Ruby, Python, Lisp, etc. and a managed VM language which uses JIT compilation, such as Java and C#. The latter often don't need to use an interpreter and in the end compile down to native machine code. More here: http://en.wikipedia.org/wiki/Interpreted_language – dodgy_coder May 02 '12 at 01:27
  • @dodgy_coder, Are you sure Java compiles down to native machine code? No, it compiles down to bytecode which is **not native** machine code. Same goes for CLR languages like C#. – Pacerier Jul 12 '15 at 14:41
  • Java and C# both include a runtime component, which JIT compiles the bytecode into native code. – dodgy_coder Jul 12 '15 at 14:46
10

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.

Hanu
  • 101
  • 1
  • 5