.Net CLR Internals

The CLR is described as the “execution engine” of .NET. It provides the environment within which the programs run. It’s this CLR that manages the execution of programs and provides core services, such as code compilation, memory allocation, thread management, and garbage collection. Through the Common Type System (CTS), it enforces strict type safety, and it ensures that the code is executed in a safe environment by enforcing code access security. The software version of .NET is actually the CLR version.

The European Computer Manufacturers Association (ECMA) standard has defines the Common Language Specification (CLS); this enforces that software development languages should be interoperable between them. The code written in a CLS should be compliant with the code written in another CLS-compliant language. Because the code supported by CLS-compliant language should be compiled into an intermediate language (IL) code. The CLR  engine executes the IL code. This ensures interoperability between CLS-compliant languages.

The ECMA standard, Common Language Infrastructure (CLI), defines the specifications for the infrastructure that the IL code needs for execution.The CLR is Implementation of CLI. The CLI provides a common type system (CTS) and services such as type safety, managed code execution and side by side execution.

In my previous article (.Net CLR Overview) we have seen how CLR is get loaded when user executes .net executable on windows (say) platform.

There are many components in CLR which are used to do specific tasks or functions of CLR.

The above diagram show the various components of .Net CLR , Each component is responsible for specific functionality mentioned below.

  • Class Loader : It is used to load all the classes (MSIL Code) at runtime into CLR. The class loader component of the CLR uses metadata to locate specific classes within assemblies, either locally or across networks.
  • MSIL to Native compiler : It is a JIT (Just In Time) compiler it will convert MSIL code to native code.
  • Code Manager : It manages the code during execution.
  • Garbage Collector : Memory allocation and Garbage collector, this performs automatic memory management.
  • Security engine : this enforces security restrictions as code level security folder level and machine level security using tools provided by Microsoft .NET and using .NET Framework setting under control panel.
  • Type Checker : It enforces strict type checking.
  • Thread Support : It provides multithreading support to .Net applications.
  • Exception Manager : It provides mechanisum to handle execptions at runtime.
  • Debug Engine : Allows developer to debug different types of applications.
  • Com Marshaler : Allows .NET applications to exchange data with COM applications.
  • Base Class library support : Which provides the classes (types) that the applications need at run time.

How it works

When the .NET program is compiled, the output of the compiler is not an executable file but a file that contains a special type of code called  the Microsoft Intermediate Language (MSIL), which is a low-level set of instructions understood by the common language run time. This MSIL defines a set of portable instructions that are independent of any specific CPU. It’s the job of the CLR to translate this Intermediate code into a executable code when the program is executed making the program to run in any environment for which the CLR is implemented. And that’s how the .NET Framework achieves Portability. This MSIL is turned into executable code using a JIT (Just In Time) complier. The process goes like this, when .NET programs are executed, the CLR activates the JIT complier. The JIT complier converts MSIL into native code on a demand basis as each part of the program is needed. Thus the program executes as a native code even though it is compiled into MSIL making the program to run as fast as it would if it is compiled to native code but achieves the portability benefits of MSIL.

We will cover CLR core features like garbage collector, Thread support,Exception manager,Debug engine,Security engine in next articles.

2 thoughts on “.Net CLR Internals

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s