a blog for those who code

Thursday, 31 July 2014

All you want to know about Common Language Run Time (CLR)

In Introduction to the .Net platform I have explained whats the difference between compilation process of .Net and any other platform. In .Net the applications are compiled to IL, which is executed by the CLR i.e. Common Language Run Time. So lets start our brief but simple introduction to CLR.

Now, before we start we should understand one thing that CLR (Common Language Runtime) is the foundation of the .Net Framework or so called heart of any .net applications.

So lets first understand why this CLR is important. If you have worked on any traditional environment before where you manually have to build security systems, implement error handling and manage memory in your code which is actually an overhead for all developers. Like C++ programmers have full access of exception handling but they have to manually manage their memory.

So, wouldn't it be awesome if I say that you don't have to worry about these things when you are building .Net applications. Yes, I know its Awesome!!! that's where the CLR comes handy, because it solves many problems by offering a feature-rich set of services that all language can use. The features are described below.

CLR Fatures :

  • Framework's Class Library : CLR contains built-in types to manage assemblies, memory, security, threading, and other runtime system support.
  • Debugging : Trust me, debugging can't be easier than in the .Net Framework and who else CLR facilitates for making it easier to debug code.
  • Execution Management : It manages the execution of code.
  • Exception Management : It allows us to write code to create and handle exceptions.
  • Garbage Collection : It automatically manages memory and execute garbage collection.
  • Just-In-Time(JIT) compilation : It ensures to compile just before executing.
  • Security : It provides a support of role-based security.
  • Thread Management : It allows you to run multiple threads of execution.
  • Type Safety : It ensure to match compatible types.


You might heard of someone saying that in .Net applications all are managed code. So you can think of CLR as an agent who manages the code with all the above features which is the fundamental principle of the runtime.  So, the code which targets the runtime is known as managed code ( ensure that code is good to be executed ) while code that does not target the runtime is known as unmanaged code.

CLR Execution Process

So, we will go through the execution process of CLR. If any application is for .Net, Windows starts up the CLR and passes the application to the CLR for execution. After that CLR will load the executable assembly, finds the entry point and begins its execution process.

Now CLR will load other referenced assemblies such as dynamic link libraries (DLLs). Note that CLR will load only those assemblies which are required at that moment, i.e. an assembly won't be loaded until the CLR needs access to the assembly's code.

Now to execute the application the CLR must translate the IL(Intermediate Language) to binary code that the operating system can understand. This is what the responsibility of JIT compiler. After the IL is compiled to machine code by the JIT compiler , the CLR holds the compiled code in a working code. So the next time when the code is executed, the CLR checks its working set and runs the code directly if its compiled.

The CLR operates on method level. Therefore, when the CLR begins execution , the JIT compiler compiles the entry point (the Main method in C#). Each subsequent method is compiled just before execution. This process of checking the working set, JIT compilation, assembly loading and execution continues until the program ends.

So in simple terms the CLR execution process consists of JIT compilation if its not compiled and then storing it in working set, loading assembly and then execute. For each method these operations are carried out until the program ends.

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment

Contact Us

Name

Email *

Message *

Sponsers

Best Hosting Plans

CC License