Read ASP.NET (Dot Net) framework interview questions and answers for beginners and experienced on DritalConnect
1. What is an IL code?
IL (Intermediate Language )code is a CPU independent partially compiled code.
2. Why IL code is not fully Compiled?
As we don't have idea what kind of environment .NET code will run. We don't know what can be the end operating system,CPU configuration, machine configuration, security configuration etc.
So the IL code is half compiled and on run-time this code is compiled into machine specific code using the environment properties (CPU,OS,machine configuration etc.)
3. Who compiles the IL code and how does it works?
IL code is compiled by the JIT (Just in time) compiler.
4. How does JIT compilation work ?
JIT Compiles the code just before execution and then saves this translation in memory.
Just before execution JIT can compile, per-file ,per function or a code fragment.
5. What are different types of JIT ?
In Microsoft .NET framework there are 3 types of JIT compilers:
Normal-JIT (Default): This type of JIT compiler compiles only those methods that are called at run time.
These methods are compiled the first time they are called, and then they are stored in the cache.
When the same method are called again, the compiled code from cache is used for execution.
Econo-JIT: This type of JIT compiler compiles only those methods that are called at run-time.
But these compiled methods are not saved into cache so that RAM memory can be utilized in an optional manner.
Pre-JIT: Pre-JIT compiles complete source code into native code in a single compilation cycle.
This is done at the time of deployment of the application.
We can implement Pre-JIT by using ngen.exe.
Normal JIT is the default compiler implemented in the .Net framework.
It produces the optimized code. Econo-JIT just replaces IL instruction with native counterpart.
It doesn't do any kind of optimization. Econo-JIT doesn't store the compiled code in cache so it requires less memory comparatively.
The choice of Normal-JIT and Econo-JIT is being decided internally. Econo-JIT is chosen when devices have limitation of memory and CPU cycle issues like windows CE powered device.
When there is no memory crunch and CPU power is higher than Normal-JIT is used.
Pre-JIT is implemented by using ngen.exe.
6. What is Native Image Generator (Ngen.exe) ?
Ngen stores full compiled .Net code into cache.
Rather than dynamically compiling the code on run-time a full image of native code is stored in cache while installing the application.
This leads to better performance as the assembly loads and execute faster.
In order to Install full compiled native code in cache we can execute the below command line from visual studio command prompt.
ngenx.exe install <assemblyname>
7. So does it mean that ngen.exe will always improve performance?
No it's not always necessary that ngen.exe produces optimized code because it uses the current environments parameters which can change over a period of time.
For instance a code compiled in windows XP environment will not be the optimized code to run under windows 2008 server.
So we need to once test with ngen and without ngen to conclude if really the performance differs.
8. What is a CLR?
CLR - Common Language Runtime
It is the heart of .Net framework and it does 4 primary important things:
- Garbage collection:
- CAS - Code Access Memmory
- CV - Code Verification
- IL to Native translation
9. What is the difference between managed and unmanaged code?
Code that executes under CLR execution environment is called as managed code. Unmanaged code executes outside CLR boundary. Unmanaged code is nothing but code written in C++, vB6, VC++ etc.
Unmanaged code codes have their own environment in which the code runs and it's completely outside the control of CLR.
10. What is garbage collector?
Garbage collector is a feature of CLR which does the cleaning of the unused managed objects and reclaims memory.It does not clean unmanaged objects.
It's a background thread which runs continuously and at a specific intervals it checks if there are any unused objects whose memory can be claimed.