managed code in .NET & Unmanaged Code

https://docs.microsoft.com/en-us/dotnet/standard/managed-code

What is Managed Code In NET? difference between managed code and unmanaged code?

Managed Code And Unmanaged Code

Managed Code In NET: Managed code is the code that is executed directly by the CLR. The applications that are created using managed code automatically have CLR services, such as type checking, security, and automatic garbage collection.

These services help to provide platform and language independence to managed code applications.

Managed Code In NET

Managed Code In Net And Unmanaged Code

The CLR compiles the applications to Microsoft Intermediate Language (MSIL) and not the machine code.

This MSIL along with the metadata that describes the attributes, classes, and methods of the code reside in an assembly.

The compilation takes place in the managed execution environment, which assures the working of the code.

managed code in net and unmanaged code

The benefits provided by the CLR can be used by selecting one or more language compilers, such as Visual Basic, C#, Visual C++, or any of the third-party compilers, such as COBOL, Perl, or Eiffel.

The language compiler determines the syntax the must be used by the code.

When you compile the code into managed code, the compiler converts the source code into MSIL, which is CPU-independent. MSIL must be converted into CPU-specific code by the JIT compiler, before the execution of the code.

When you compile your source code into MSIL, the required metadata is generated.

The metadata contains the definition of types, member signature, the members in the code, and other details that the code uses at the time of execution.

The runtime locates and extracts the metadata from the file during execution. While executing the application, a JIT compiler translates the MSIL into native code.

After compiling, the code is passed through a verification process that examines the MSIL and metadata to Check whether the code is safe, such as it should be able to access only those memory locations which it is authorized to access.

The runtime also supports another method of compilation named install-time code generation.

It converts MSIL into native code in a single shot by taking a larger unit of code at a time The entire assembly is converted into native code at the installation time itself.

By default, it takes care of other assemblies referred in this assembly. It stores the resulting native code for use when the assembly is loaded and run.

Using the install-time code generation the entire assembly (that is already installed) is converted into native code that results in loading the code file more rapidly.

The CLR provides the environment to safely execute the code. This process of compilation and execution of the code by the JIT compiler is repeated until the execution is complete.

Throughout the execution, the managed code receives services, such as garbage collection security, interoperability with unmanaged code, cross-language debugging support, and enhanced deployment and versioning support.

difference between managed code in net and unmanaged code

Unmanaged Code in .NET

In contrast to the managed code, unmanaged code directly compiles to the machine code and runs on the machine where it has been compiled.

It does not have services, such as security or memory management, which are provided by the runtime.

If your code is not security-prone, it can be directly interpreted (or manipulated) by any user, which can prove harmful.

Memory Management

One of the most important services that the CLR provides during managed execution is the automatic memory management.

The CLR uses the garbage collector to manage the allocation and release of memory for an application.

It is one of the advantages of CLR that is beneficial for the developers since it reduces the burden of writing codes to manage memory while developing managed applications.

Automatic memory management removes common problems, such as forgetting to free an object which

results in a memory leak, or attempting to access memory for an object that is already free.

Managed Code In .NET | Unmanaged Code

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *