Common Language Runtime
Common Language Runtime
The Common Language Runtime (CLR) is a core component of .NET Framework that manages the
execution and the lifecycle of all .NET applications (code). It provides various services, including
automatic memory management, exception handling, security, and type safety. When a .NET
application is compiled, it generates an intermediate language code called Common Intermediate
Language (CIL). The CLR is responsible for translating this CIL into machine code and managing the
execution of the resulting program. The CLR also provides a platform for interoperability between
different programming languages that target the .NET Framework. This means that a program
written in one .NET language can easily use libraries written in another .NET language. Overall, the
CLR is an essential component of the .NET Framework that enables developers to create robust,
secure, and interoperable applications.
As part of the Microsoft .NET Framework, the Common Language Runtime (CLR) is the
programming (Virtual Machine component) that manages the execution of programs
written in any language that uses the .NET Framework, for example, C#, VB.Net, F# and so
on.
Programmers write code in any language, including VB.Net, C#, and F#, when they compile
their programs into an intermediate form of code called CLI in a portable execution file (PE)
that can be managed and used by the CLR. Then the CLR converts it into machine code to
be will be executed by the processor.
The information about the environment, programming language, its version, and what
class libraries will be used for this code are stored as metadata with the compiler that tells
the CLR how to handle this code.
The CLR allows an instance of a class written in one language to call a method of the class
written in another language.
Exception handling
Type safety
Security
Improved performance
Language independency
Platform independency
Architecture independency
MSIL to Native code - The Just In Time (JTI) compiler will convert MSIL code into native
code.
Code Manager - It manages the code at run time.
Garbage Collector - It manages the memory. Collect all unused objects and deallocate
them to reduce memory.
Performance improvements.
Support for explicit free threading that allows the creation of multithreaded, scalable
applications.
Garbage collection.
Use of delegates instead of function pointers for increased type safety and security.
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).
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.