The Common Language Runtime in the .NET Framework is the Virtual Machine component that handles program execution for various languages such as C#, F#, Visual Basic .NET, etc. The managed execution environment is provided by giving various services such as memory management, security handling, exception handling, garbage collection, thread management, etc.
The Common Language Runtime implements the VES (Virtual Execution System) which is a run time system that provides a managed code execution environment. The VES is defined in Microsoft’s implementation of the CLI (Common Language Infrastructure).
Architecture of Common Language Runtime
A diagram that demonstrates the architecture of Common Language Runtime is given as follows:
There are multiple components in the architecture of Common Language Runtime. Details about these are given as follows:
- Base Class Library Support: The Common Language Runtime provides support for the base class library. The BCL contains multiple libraries that provide various features such as Collections, I/O, XML, DataType definitions, etc. for the multiple .NET programming languages.
- Thread Support: The CLR provides thread support for managing the parallel execution of multiple threads. The System.Threading class is used as the base class for this.
- COM Marshaller: Communication with the COM (Component Object Model) component in the .NET application is provided using the COM marshaller. This provides the COM interoperability support.
- Type Checker: Type safety is provided by the type checker by using the Common Type System (CTS) and the Common Language Specification (CLS) that are provided in the CLR to verify the types that are used in an application.
- Exception Manager: The exception manager in the CLR handles the exceptions regardless of the .NET Language that created them. For a particular application, the catch block of the exceptions are executed in case they occur and if there is no catch block then the application is terminated.
- Security Engine: The security engine in the CLR handles the security permissions at various levels such as the code level, folder level, and machine level. This is done using the various tools that are provided in the .NET framework.
- Debug Engine: An application can be debugged during the run-time using the debug engine. There are various ICorDebug interfaces that are used to track the managed code of the application that is being debugged.
- JIT Compiler: The JIT compiler in the CLR converts the Microsoft Intermediate Language (MSIL) into the machine code that is specific to the computer environment that the JIT compiler runs on. The compiled MSIL is stored so that it is available for subsequent calls if required.
- Code Manager: The code manager in CLR manages the code developed in the .NET framework i.e. the managed code. The managed code is converted to intermediate language by a language-specific compiler and then the intermediate language is converted into the machine code by the Just-In-Time (JIT) compiler.
- Garbage Collector: Automatic memory management is made possible using the garbage collector in CLR. The garbage collector automatically releases the memory space after it is no longer required so that it can be reallocated.
- CLR Loader: Various modules, resources, assemblies, etc. are loaded by the CLR loader. Also, this loader loads the modules on demand if they are actually required so that the program initialization time is faster and the resources consumed are lesser.