Managed and Unmanaged Code : Key Differences
This is a continuation of our previous blog on Unsafe code. In this blog, we will understand the key differences between managed and unmanaged code in C#. Unmanaged code is nothing but unsafe code.
If you recall, in C#, a code typically is run under the control of the Common Language Runtime (CLR) of the .NET frameworks. CLR helps in running the code and also provides a variety of services to make the development process easy. All the codes that are controlled by the CLR are called the Managed Code, while the parts of the program which are written using the unsafe keyword are called Unmanaged Code. These are beyond the control of CLR of the .NET frameworks.
Table of contents
- What is Managed Code in C#?
- Execution of Managed Code
- Advantages of Managed Code
- Disadvantages of Managed Code
- What is Unmanaged Code in C#?
- Execution of Unmanaged Code
- Advantages of Unmanaged Code
- Dis-advantages of Unmanaged Code
- Difference between Managed and Unmanaged code
- Conclusion
What is Managed Code in C#?
As we have seen in the beginning, the code which is developed using the Common Language Runtime (CLR) of the .NET framework is known as Managed Code. In other words, it is the code that gets executed directly in C#. The runtime environment for managed code provides a variety of services to the programmer. The services provided are exception handling, garbage collection, type checking, etc. The above-mentioned services are provided to the programmer automatically. Apart from the above-mentioned services the runtime environment also provides memory allocation, object disposal, object creation, etc.
Execution of Managed Code
The managed code is always compiled by the CLR into an intermediate language known as MSIL and after that, the executable of the code is created. The intermediate language (MSIL) in the source code is compiled by the compiler named ‘Just In Time Compiler’ when the executable is run by the programmer. Here the whole process is carried under a runtime environment, hence the runtime environment is the reason behind the working of the program.
Advantages of Managed Code
- Security of the code is improved as we are using a runtime environment, which protects from buffer overflow by checking memory buffers.
- Garbage collection is automatically implemented.
- Dynamic type checking or runtime type checking is also provided
- The runtime environment does the reference checking too. It checks if the reference point of the object is valid or not and also checks if the duplicate object is present or not.
Disadvantages of Managed Code
- Memory cannot be directly allocated.
- In managed code, low-level access to the CPU architecture cannot be obtained.
What is Unmanaged Code in C#?
The Unmanaged Code is also known as Unsafe Code. Unmanaged code depends on the computer architecture as it aims for the processor architecture. In C#, the activities like managing stacks, allocation, and release of memory, etc. are taken care of by the CLR and hence these activities are out of the purview of the programmer. The programmer tells the compiler that the management of the code will be done by him/her when he/she uses the keyword “unsafe”. However, issues like memory leaks can happen if a programmer writes bad code.
Execution of Unmanaged Code
For executing unmanaged code wrapper classes are used in C#. In C# the unmanaged code is directly executed by the operating system. Generally, the executable files of unmanaged or unsafe code are in the form of binary images which are loaded into the memory. The steps of execution of the code can be seen in the above image.
Advantages of Unmanaged Code
- Unsafe code increases stability and the performance of the program
- Low-level access to the programmer is provided
- Unsafe code provides a medium to interface with the memory
Disadvantages of Unmanaged Code
- Security is not provided to the application
- The programmer has to do exception handling
- No automatic implementation of garbage collection
- Making use of unsafe code can lead to errors that might occur as type checking is bypassed.
Difference between managed and unmanaged code?
- Managed code is the one that is executed by the CLR of the .NET framework while unmanaged or unsafe code is executed by the operating system.
- The managed code provides security to the code while undamaged code creates security threats.
- In unsafe or unmanaged code the unsafe modifier is used to write the block of code while any other code written outside the unsafe code block is managed code.
- Memory buffer overflow problems do not occur in managed code as it is taken care of by the runtime environment but these problems occur in unmanaged code.
- Runtime services are provided in managed code while they are not provided in unmanaged code.
- Source code is first converted to intermediate language and then to native language in managed code while it is directly converted into the native language in unmanaged code.
- Unmanaged code provides low-level access while managed code does not provide that.
- In unsafe or unmanaged code the unsafe modifier is used to write the block of code while any other code written outside the unsafe code block is managed code.
Conclusion
This article gives you an insight into Managed and Unmanaged code in C#. The flow of execution of each type of code has been demonstrated in the article which helps you in understanding the appropriate usage of those codes in developing any kind of application.