Common Language Runtime (CLR)
Today in Engineer's World, we are discussing a very important topic of .NET in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS
Click imagination hunt to see latest Blogs
Previous - Just In Time (JIT)
How CLR is Define?
CLR is Common Language Runtime. CLR is the execution engine that performs the task to convert the IL Code to the Machine Specific Code.
It works as a layer between Operating Systems and the applications written in .NET languages that conforms to the Common Language Specification (CLS). The main function of CLR is to convert the Managed Code into native code and then execute the program. The Managed Code compiled only when it needed, that is it converts the appropriate instructions when each function is called. Just In Time (JIT) compilation converts Intermediate Language (IL) to native code on demand at application run time which is a part of it.
During the execution of the program, CLR manages memory, thread execution, garbage collection, exception handling, CTS, code safety, and other system services. It defines the CTS as a standard type system used by all .NET languages. That means all .NET programming languages uses the same representation for common Data Types, so CLR is a language-independent runtime environment. The CLR environment is also referred to as a managed environment, because during the execution of a program it also controls the interaction with the Operating System.
What is the Complete Architectural Working of CLR?
CLR is a part of .Net Framework. You can read the complete working of .Net Framework in my previous Blog. However, in this Blog we will concentrate only on the working of CLR. Go through Figure 1.
Now, what happen when our source code is executed successfully by its language compiler, the assembly is loaded into the CLR. CLR serve as an execution engine for the IL code whose responsibility is to check the functioning of the code on the behalf of CLS and CTS. CLS define set of rules which every .NET compliant languages must follow whereas CTS uses the same representation for common Data Types. There are many more components that reside within CLR like Garbage Collection, Memory Management, Code Access Right, Type-Safety, Exception Handling, and many more which we will talk about later in this blog. Now, when the requirements are met. The CLR then transfer the managed code to JIT. JIT at run-time convert the code into user machine specific code. And when we double click on the IL Code the JIT convert it into machine which is actually getting executed according to the client machine configuration.
What are the various components that go within CLR?
The Components are listed as:
o Code Execution: Code executions involve using the source code (in IL format) and execute it according to the machine configuration.
o Common Type System (CTS): CTS uses the same representation for common Data Types.
o Automatic Memory Management: Working on an application involves creation of variables and continuously storing them on heap memory. But doing this manually is not as easy task, as easily we are saying. So, it is also taken care by CLR automatically.
o JIT Compiler: JIT is a tool provided by the CLR which give a way to the Source Code so that it can perform on the Machine efficiently.
o Garbage Collector: It is used to free the memory space that it unknowingly used by some object in heap memory. GC can run manually as well as automatically.
o Security Manager: CLR implements a mechanism that operates on the concept granting permissions on the code's origin. Using security manager, it's possible to limit the resources to access code.
o Access to Metadata: When creating an application, you might want to make sure that people could find out information. You can accomplish this task by adding metadata to the any document type (for e.g. pdf or word file).
o Class Loader: For an application, CLR load the class that you create (or base class library that you referred), prepare them for use, and then either execute them or assist them to use either in their designed time use.
o Exception Handling: Handling of exceptions that occur either at Compile-time or Design-time. Including Cross-Language Exception.
o Conversion of IL to Native Code
o Verification of Type-Safety: Interpreting of the code at compile time by the help of Intellisense. Type safety also comes into picture when you assign one type of variable to another.
How CLR is considered to be as an important organ of .NET Framework?
The Various Advantages are listed below:
o High Performance Application: As you can see the components that CLR provide is all justifying its high performance.
o Vastly Simplified Document: All the application is integrated with features like type-safe, data compatibility.
o Seamless Integration of the Code Written in Various Languages: CLR gave us a component i.e. CTS, which resolves the languages conflict and make the system Language Interoperable.
o Evidence-Based Security with Code Identity: It grants the creator of original work exclusive rights to its use and distribution.
o Assembly-Based Deployment that eliminates DLL HELL
o Side-By-Side Versioning of Reusable Components: By the involvement of CLR, we can have multiple version of code execution at the same time.
o Code Reuse through Implementation Inheritance: Inheriting of classes is not possible till Component Object Model. But, by the involvement of OOP, code reusing make task easy.
o Automatic object management: GC provided by CLR clean unused managed object and reclaims the memory.
For any query, comment us below.
Next - .Net Namespaces
Click imagination hunt to see latest Blogs
Keep learning and sharing...