Understanding Code Management in .NET
In order to understand how .NET processor handles the code and how code is optimized, we should know about the .NET language runtime. .NET platform consists of a runtime known as Common Language Runtime or CLR. It provides the environment which supports the execution of code and other management tasks like memory management, garbage collection and multithreading functionality.
CLR is also responsible for Common Type System which provides the flexibility of choosing any .NET language and still CLR being the common runtime for any of the .NET based language. It also implies that output of any .NET program will be decided by the runtime and it would not be based on the language compiler.
In .NET, code management is done in two phases. First is application development phase and second one is application execution phase. We have managed code which targets the runtime for starting itself. For doing this runtime must be active in a process. This is done by a runtime host which loads it in a process. After successful loading of runtime application domains are created which are used further for the loading of actual code.
.NET compilers of .NET based languages transform the source code into IL or Microsoft Intermediate language which is a processor independent common language. With MSIL compilers also produce metadata which has the information about the types and references used in the code and other things which may be used by the runtime at the time of execution.
After the code is compiled to MSIL it is converted into processor specific executable code or native code by Just-in-Time Compiler or simply JIT compiler. CLR offers three kinds of JIT compilers. There is a JIT compiler which transforms the MSIL code into native code with the optimizations. Other is Main JIT which does the same conversion as the JIT but without optimizations. Third class is of PreJIT compilers which are based on Main JIT compilers and these perform the translation of MSIL code into native code at the compilation time.
One of the
other important services provided by runtime in order to maintain managed
execution of code is automatic memory management. This simple implies
that we do not need to write code for doing memory management related
work like freeing an object while developing the application. The garbage
collector of the CLR performs these tasks of allocating memory space to
a new process or object and releasing memory of the objects which are
no longer used by the application automatically.