What is Common Language Runtime (CLR)?

What is CLR?

Common Language Runtime (CLR) is a run-time environment that handles the execution of .NET programs through several services in order to make the development process simpler. It implements the Virtual Execution System for a managed code execution environment. 

The main functions of the Common Language Runtime are:

  • Translation of the written code into machine language code for the CPU to execute further
  • Memory management
  • Thread management
  • Exception handling
  • Security handling
  • Garbage collection
  • Provides several language features

To go through some of the most frequently asked interview questions on C#, you can visit this: Link

Components of CLR:-

The multiple components of Common Language Runtime are illustrated below:

  • Base Class Library Support: The base class library is supported by the Common Language Runtime. For the various.NET programming languages, the BCL includes several libraries that provide various features such as XML, datatype definitions, built-in types, arrays, exceptions, math libraries, basic File I/O, collections, reflection, networking, string manipulation, and more.
     
  • Thread Support: Thread support is provided by the CLR, which enables several threads to run in parallel. The base class used for this is the System. Threading class.
     
  • COM Marshaller: The COM marshaller is used to communicate with the COM (Component Object Model) component in the.NET framework. This offers support for COM interoperability.
    Marshaling is the method of bridging managed and unmanaged code. We call .NET code managed because the CLR manages it. Unmanaged code is code that is not regulated by the CLR. It transports messages from the managed to unmanaged environment and back. It is one of the CLR's most vital services. Since several types in an unmanaged environment have no equivalents in a managed environment, conversion protocols must be written to transform managed types to unmanaged and vice versa; this is the marshaling method.
     
  • Type Checker: The type checker ensures type safety by checking the types used in an application using the Common Type System (CTS) and the Common Language Specification (CLS) given in the CLR. They should match the standards provided by the CLR.
     
  • Exception Manager: To prevent application failure, the CLR's exception manager manages all exceptions, regardless of which .NET language caused them. If an exception occurs in a specific application, the catch block is executed, and if there is no catch block, the application is terminated. If a runtime error (exception) emerges in a program, the exception manager in the CLR (Common Language Runtime) first determines the type of error that occurred in the program, then generates an object of the exception class related to that error and throws that object, which will abnormally end the program on the line where the error occurred and show the error message related to that class.
     
  • Security Engine: The CLR's security engine manages security permissions at multiple levels, including code, folder, and system levels. This is achieved with the aid of the numerous methods of the .NET framework.
     
  • Debug Engine: The CLR makes debugging simpler. The debug engine can be used to debug an application while it is running. It will enable the debugger utility, allowing the developer to make changes line by line without interrupting the application's execution. There are many ICorDebug interfaces that are used to monitor the controlled code of the debugged application.
     
  • JIT Compiler: The Just-In-Time compiler (JIT) is a component of the Common Language Runtime in.NET that manages the execution of .NET programs in any.NET programming language. The source code is translated to the intermediate language by a language-specific compiler. The Just-In-Time (JIT) compiler then translates this intermediate language into machine code. The machine code produced by the JIT compiler is unique to the computer environment in which it runs. The compiled Microsoft Intermediate Language(MSIL) or Common Intermediate Language(CIL) is saved and made available for future calls if necessary.
     
  • Code Manager: The managed code built in the.NET framework is managed by the code manager in CLR during execution run-time. A language-specific compiler translates managed code to an intermediate language, and then the JIT compiler translates the intermediate language code to the machine code.
     
  • Garbage Collector: The garbage collector in CLR makes automated memory management possible. When memory space is no longer needed, the garbage collector frees it so that it can be reallocated to a new application. 
    The garbage collector allocates a segment of memory to store and handle objects after the CLR initializes them. In comparison to a native heap in the operating system, this memory is referred to as the managed heap. Each managed process has its own managed heap. On the same heap, all threads in the loop assign memory for objects. The garbage collector reclaims the memory that has been filled by dead items when a garbage collection is activated. The reclaiming method compacts live objects, allowing them to be transferred together and eliminating dead space, resulting in a smaller heap. This keeps items that are allocated together on the managed heap together, ensuring their locality.
     
  • CLR Loader: The CLR loader loads a number of modules, tools, assemblies, and other objects. Furthermore, this loader does not automatically load modules but loads them only when they are actually needed, resulting in a faster program initialization time and lower resource consumption. 
    Since assemblies and modules are loaded on demand, the parts of a program that aren't used are never loaded into the memory. It also means that, as the types found in those files are required during execution, a running application will often see new assemblies and modules loaded over time. You have two choices if this isn't the action you want. One solution is to declare hidden static fields of the types you want to ensure are loaded when your type is loaded. The other alternative is to directly communicate with the loader.
    Loading is usually activated by the just-in-time (JIT) compiler in the CLR based on types. When converting a method body from CIL to machine code, the JIT compiler requires access to the declaring type's type definition as well as the type definitions for the type's fields. Furthermore, the JIT compiler requires access to any form definitions used by any local variables or parameters of the JIT-compiled process. When you load a type, you're also loading the assembly and module that comprise the type specification.

CLR Versions:

.NET Version number CLR version
1.0 1.0
1.1 1.1
2.0 2.0
3.0 2.0
3.5 2.0
4.0 4
4.5 4
4.5.1 4
4.5.2 4
4.6 4
4.6.1 4
4.6.2 4
4.7 4
4.7.1 4

Benefits of CLR:

Following are the benefits of Common Language Runtime:

  • It boosts performance by allowing for rich interaction among programs during runtime.
  • It enhances portability by eliminating the need to recompile a program on any supported operating system.
  • It supports the use of components built using other.NET programming languages.
  • With the assistance of Garbage Collector, it supports automatic memory management. Programmers would have to develop memory management codes if there had been no garbage collector, which would be some kind of burden for them.
  • As it analyses the MSIL instructions to see whether they are secure or not, protection improves. In addition, using delegates instead of function pointers increases type safety and security.
  • Since CTS within CLR offers a shared standard that allows different languages to expand and share each other's libraries, it enables cross-language integration.
  • It provides flexibility in terms of language, forum, and architecture.
  • It makes it simple to create scalable and multithreaded applications because the developer doesn't have to worry about memory management or security.
Avatar

Contributor

ghostrider
An Avid Tech Blogger and Marketer.

Previous Posts:-