none
Managed Code Vs Unmanaged Code? RRS feed

  • Question

  • I have read a lot on managed code and unmanaged code,now what I could understand is that any code that targets the .net framework is Managed code (right?),which inturn means its under the control of the CLR @ runtime.

    Assuming the above as true, I  suppose unmanaged code does not target the framework???? or it does not come under the control of CLR @ runtime?

    Can somebody explain this ? if possible with code example  so that I can undersand the difference...thx.

     

    Saturday, February 10, 2007 4:24 AM

Answers

    • Managed Code

      Managed code is code that is written to target the services of the managed runtime execution environment (like Common Language Runtime in .NET Framework). The managed code is always executed by a managed runtime execution environment rather than the operating system directly. Managed refers to a method of exchanging information between the program and the runtime environment. Because the execution of code is governed by the runtime environment, the environment can guarantee what the code is going to do and provide the necessary security checks before executing any piece of code. Because of the same reason the managed code also gets different services from the runtime environment like Garbage Collection, type checking, exception handling, bounds checking, etc. This way managed code does not have to worry about memory allocations, type safety, etc. Applications written in Java, C#, VB.NET, etc target a runtime environment which manages the execution and the code written using these types of languages is known as Managed Code. Managed code is always compiled into an Intermediate Language (MSIL in case of .NET Framework). The compiler used by .NET framework to compile managed code compiles it into Intermediate Language and generates the necessary metadata, symbolic information that describes all of the entry points and the constructs exposed in the Intermediate Language (e.g., methods, properties) and their characteristics. The Common Language Infrastructure (CLI) Standard describes how the information is to be encoded, and programming languages that target the runtime emit the correct encoding.

      In .NET Framework Managed Code runs within the .Net Framework’s CLR and benefits from the services provided by the CLR. When we compile the managed code, the code gets compiled to an intermediate language (MSIL) and an executable is created. When a user runs the executable the Just In Time Compiler of CLR compiles the intermediate language into native code specific to the underlying architecture. Since this translation happens by the managed execution environment (CLR), the managed execution environment can make guarantees about what the code is going to do, because it can actually reason about it. It can insert traps and sort of protection around, if it's running in a sandboxed environment, it can insert all the appropriate garbage collection hooks, exception handling, type safety, array bounce, index checking and so forth.

      Managed code also provides platform independence. As the managed code is first compiled to intermediate language, the CLR’s JIT Compiler takes care of compiling this intermediate language into the architecture specific instructions.
    • Unmanaged Code

      Code that is directly executed by the Operating System is known as un-managed code. Typically applications written in VB 6.0, C++, C, etc are all examples of unmanaged code. Unmanaged code typically targets the processor architecture and is always dependent on the computer architecture. Unmanaged code is always compiled to target a specific architecture and will only run on the intended platform. This means that if you want to run the same code on different architecture then you will have to recompile the code using that particular architecture. Unmanaged code is always compiled to the native code which is architecture specific. When we compile unmanaged code it gets compiled into a binary X86 image. And this image always depends on the platform on which the code was compiled and cannot be executed on the other platforms that are different that the one on which the code was compiled. Unmanaged code does not get any services from the managed execution environment.

      In unmanaged code the memory allocation, type safety, security, etc needs to be taken care of by the developer. This makes unmanaged code prone to memory leaks like buffer overruns and pointer overrides and so forth.

      Unmanaged executable files are basically a binary image, x86 code, loaded into memory. The program counter gets put there and that’s the last the Operating System knows. There are protections in place around memory management and port I/O and so forth, but the system doesn’t actually know what the application is doing.

    Hope to make it clearer, if you have any further problem, pls feel free to let us know, thank you.

    Monday, February 12, 2007 8:43 AM
  • Hi,

    If you want to understand managed code, you'd first understand what is the .net framework, here is a good reference for you: http://www.codeproject.com/dotnet/DotNetWhitePaper.asp

    Managed code: The .NET framework provides several core run-time services to the programs that run within it - for example exception handling and security. For these services to work, the code must provide a minimum level of information to the runtime. Such code is called managed code.

    Managed data: This is data that is allocated and freed by the .NET runtime's garbage collector.

    Managed classes: This is usually referred to in the context of Managed Extensions (ME) for C++. When using ME C++, a class can be marked with the __gc keyword. As the name suggests, this means that the memory for instances of the class is managed by the garbage collector, but it also means more than that. The class becomes a fully paid-up member of the .NET community with the benefits and restrictions that brings. An example of a benefit is proper interop with classes written in other languages - for example, a managed C++ class can inherit from a VB class. An example of a restriction is that a managed class can only inherit from one base class.

    Thanks

     

    Monday, February 12, 2007 8:38 AM
  • Unmanaged Code example like C++, Win32, and COM which compiled into native so they not managed by .net runtime

    but as you spend alot of time and money to build them .Net provide Unmanaged Interoperability

    so you can use them into your application and  here the word Managed are came into the picture

    ie the runtime allow to use them but it said i will not managed them its your job to manage them

    one service that runtime provide to managed code is Garbage Collector its know how to control the lifetime of .net object and memory

    but  it did not know how to clear resource allocated by unmanaged code. so this called unmanaged code

    and this your responsibility to clear the resource allocated by unmanaged code via implementing IDisposable interface for example

    Saturday, February 10, 2007 12:04 PM

All replies

  • Unmanaged Code example like C++, Win32, and COM which compiled into native so they not managed by .net runtime

    but as you spend alot of time and money to build them .Net provide Unmanaged Interoperability

    so you can use them into your application and  here the word Managed are came into the picture

    ie the runtime allow to use them but it said i will not managed them its your job to manage them

    one service that runtime provide to managed code is Garbage Collector its know how to control the lifetime of .net object and memory

    but  it did not know how to clear resource allocated by unmanaged code. so this called unmanaged code

    and this your responsibility to clear the resource allocated by unmanaged code via implementing IDisposable interface for example

    Saturday, February 10, 2007 12:04 PM
  • Hi,

    If you want to understand managed code, you'd first understand what is the .net framework, here is a good reference for you: http://www.codeproject.com/dotnet/DotNetWhitePaper.asp

    Managed code: The .NET framework provides several core run-time services to the programs that run within it - for example exception handling and security. For these services to work, the code must provide a minimum level of information to the runtime. Such code is called managed code.

    Managed data: This is data that is allocated and freed by the .NET runtime's garbage collector.

    Managed classes: This is usually referred to in the context of Managed Extensions (ME) for C++. When using ME C++, a class can be marked with the __gc keyword. As the name suggests, this means that the memory for instances of the class is managed by the garbage collector, but it also means more than that. The class becomes a fully paid-up member of the .NET community with the benefits and restrictions that brings. An example of a benefit is proper interop with classes written in other languages - for example, a managed C++ class can inherit from a VB class. An example of a restriction is that a managed class can only inherit from one base class.

    Thanks

     

    Monday, February 12, 2007 8:38 AM
    • Managed Code

      Managed code is code that is written to target the services of the managed runtime execution environment (like Common Language Runtime in .NET Framework). The managed code is always executed by a managed runtime execution environment rather than the operating system directly. Managed refers to a method of exchanging information between the program and the runtime environment. Because the execution of code is governed by the runtime environment, the environment can guarantee what the code is going to do and provide the necessary security checks before executing any piece of code. Because of the same reason the managed code also gets different services from the runtime environment like Garbage Collection, type checking, exception handling, bounds checking, etc. This way managed code does not have to worry about memory allocations, type safety, etc. Applications written in Java, C#, VB.NET, etc target a runtime environment which manages the execution and the code written using these types of languages is known as Managed Code. Managed code is always compiled into an Intermediate Language (MSIL in case of .NET Framework). The compiler used by .NET framework to compile managed code compiles it into Intermediate Language and generates the necessary metadata, symbolic information that describes all of the entry points and the constructs exposed in the Intermediate Language (e.g., methods, properties) and their characteristics. The Common Language Infrastructure (CLI) Standard describes how the information is to be encoded, and programming languages that target the runtime emit the correct encoding.

      In .NET Framework Managed Code runs within the .Net Framework’s CLR and benefits from the services provided by the CLR. When we compile the managed code, the code gets compiled to an intermediate language (MSIL) and an executable is created. When a user runs the executable the Just In Time Compiler of CLR compiles the intermediate language into native code specific to the underlying architecture. Since this translation happens by the managed execution environment (CLR), the managed execution environment can make guarantees about what the code is going to do, because it can actually reason about it. It can insert traps and sort of protection around, if it's running in a sandboxed environment, it can insert all the appropriate garbage collection hooks, exception handling, type safety, array bounce, index checking and so forth.

      Managed code also provides platform independence. As the managed code is first compiled to intermediate language, the CLR’s JIT Compiler takes care of compiling this intermediate language into the architecture specific instructions.
    • Unmanaged Code

      Code that is directly executed by the Operating System is known as un-managed code. Typically applications written in VB 6.0, C++, C, etc are all examples of unmanaged code. Unmanaged code typically targets the processor architecture and is always dependent on the computer architecture. Unmanaged code is always compiled to target a specific architecture and will only run on the intended platform. This means that if you want to run the same code on different architecture then you will have to recompile the code using that particular architecture. Unmanaged code is always compiled to the native code which is architecture specific. When we compile unmanaged code it gets compiled into a binary X86 image. And this image always depends on the platform on which the code was compiled and cannot be executed on the other platforms that are different that the one on which the code was compiled. Unmanaged code does not get any services from the managed execution environment.

      In unmanaged code the memory allocation, type safety, security, etc needs to be taken care of by the developer. This makes unmanaged code prone to memory leaks like buffer overruns and pointer overrides and so forth.

      Unmanaged executable files are basically a binary image, x86 code, loaded into memory. The program counter gets put there and that’s the last the Operating System knows. There are protections in place around memory management and port I/O and so forth, but the system doesn’t actually know what the application is doing.

    Hope to make it clearer, if you have any further problem, pls feel free to let us know, thank you.

    Monday, February 12, 2007 8:43 AM
  •  

    thanks for the replies,

    I get the difference now! but this has made me wonder a little, how is it possible to write unmanged code in .net?

    When ever you write down some code in any .NET IDE ( i use Visual C# Express Edition) it will eventually target the .Net framework when you build it right?

    And I have never seen any code in VB6 or C in any .Net  IDE  (not possible at all )

    So my question is 'Why really bother about unmanaged code when you cant write it in c#?'  ,I guess you can only reference a project  to a vb or a c file?

     

     

    Tuesday, February 13, 2007 1:55 AM
  • Most of time you use Unmanaged Code already exist because you do not want to waste your money and time

    Another reason you may write Unmanaged code if you need a feature that is not avilabe through .Net Framework

    and it is  avilable in windows API  for example

    Tuesday, February 13, 2007 2:07 AM
  • Yes, also .net provide interoperation with the unmanaged code as mentioned previously.
    Tuesday, February 13, 2007 2:44 AM
  • Can you give a small example on how this is implemented ?

    And is it possible to find out if the code your currently executing is running as managed or un-managed (Some method in System.Reflection perhaps?)

    thanks,

     

     

    Thursday, February 15, 2007 2:44 AM
  • Hi,

    For .net interop you can reference to: http://www.dotnetinterop.com/

    Otherwise, I recommend you separate further questions in new threads, in that way, more people can see and answer it.

    Thank you for understanding. 

    Thursday, February 15, 2007 3:50 AM
  • When ever you write down some code in any .NET IDE ( i use Visual C# Express Edition)

    It is very misleading to say ".NET IDE". I assume that VS is currently written using .Net but that does not limit it to .Net, just as applications written using .Net can process any data. Visual Studio can be used for nearly any language. Until recently it did not support Java but now it even does that. Visual Studio existed for many years before .Net did.

    it will eventually target the .Net framework when you build it right? 

    No, not everything will use, or be used by, .Net. Many developers use Visual Studio to develop C++ and even C programs and even assembly language programs that are compiled into machine language. I do.



    Sam Hobbs
    SimpleSamples.Info


    Friday, April 29, 2016 7:55 AM
  • There are many ways to do things. Sometimes it is a matter of personal preference, but in this case it is more like corporate preference.

    One good explanation for why not everything is done using .Net and/or the Java environment is that there is not enough advantage. There are important advantages but not enough to compel everyone.

    One advantage of unmanaged code is that since it is compiled to machine language, it is difficult to reverse-engineer.

    Until a year ago, when Microsoft made .Net open-source, .Net was not available to everyone. See IBM Bluemix Welcomes Microsoft's .Net - InformationWeek; IBM could not use .Net with Bluemix before .Net became open-source.

    IBM has designed and developed many languages, most before Microsoft existed. IBM has developed many operating systems, very many before Microsoft existed. Microsoft and .Net has been very successful but many young developers don't understand that they do not dominate as much as many of them think.

    IBM's Bluemix is a cloud service like Microsoft Azure is. Did you know that as of about a year ago, Bluemix had 49 Data Centers whereas Azure had about 17 Data Centers? The point is that Microsoft is not as dominant as many believe and in the IBM world .Net is not the most popular.

    Microsoft would not have been as successful as it is now without IBM. IBM was a leader before Microsoft and they are still a leader in many ways. They are highly experienced in tools other than .Net and remain successful with the other tools.

    .Net is very useful and powerful but not enough to push everything else away entirely.



    Sam Hobbs
    SimpleSamples.Info

    Friday, April 29, 2016 8:32 AM
  • Please do not necro 9 year old threads that have been marked as answered. Thanks :)
    Friday, April 29, 2016 11:59 AM