Saturday, November 29, 2008

Common Language Runtime Beginner Series Part 1:-CLR Basics

CLR or Common Language Runtime is a very important part of whole .net Framework and its utter importance is not seen by most of us as it works behind the scenes.This beginner series of my articles will help you to know what actually CLR is in simple terms and what are its basic features.

Remember this is for beginners, or people who are not as much familiar with CLR.

I will try to make this series of articles very simple so that everyone can understand and here and there i will make some simple comparisons so that the fact is understood easily.In simplifying things some facts may sound vague so please pardon me...

  • In earlier days of programming there was no concept of managed code,there was only unmanaged code which means that all the things should be handled by the operating system.
  • The programming languages compiled the programs written in them to the native code specific to the architecture or the CPU.
  • There are many disadvantages of such an approach which we will see one by one in a series of articles.
  • So first of all what is CLR....Actually speaking it can be considered as a small processing unit of its own which when given a Intermediate Language code can produce a native code to be run on that CPU.
  • CLR consists of various parts in which each is a topic of research and discussion and requires an in depth analysis.
  • So the process in which our programs get executed is first of all we have a code in a language it can be VB,C# or C++ now we compile this code using the respective compiler for that language.
  • After this first compilation we receive a IL or Intermediate Language code, now this IL code act as input to the CLR and its Just In Time Compiler chooses the right Model to be executed for compilation for that CPU type.
  • So now the JIT decides according to the CPU type and architecture which mode of compilation it should follow.
  • Now after this decision JIT actually compiles and produces the native code which can be run on that CPU.
  • CLR can be considered as a SandBox environment from which no code can execute outside its given bounds that is the code execution is confined and this is a major advance over the earlier model of program execution.
  • So now you can say that .net can only execute the managed code and there is no need for unmanaged code but this is not true. There are some special cases where  unmanaged code is the best thing till today e.g are device drivers and real time applications where performance is a bottleneck although there are algorithms which will be implemented in future CLR for support of Real Time applications.
  • But .net framework also provides few ways to write unmanaged code which will not use facilities of CLR and execute in an old fashioned way.
  • Both C++ and C# has ways to execute this unmanaged code.
  • When the code is first compiled to an IL we get a .exe or .dll file this is what we call an assembly.
  • Once you ask this assembly to be executed it is loaded into the ApplicationDomain or AppDomain of the CLR.
  • AppDomain of CLR is the region in which a process is running and each assembly can get executed without creating a new process side by side with other assemblies without hampering or affecting their execution.
  • Creating separate processes for execution was a performance intensive task which earlier programming languages used to do.But with CLR this performance lapse is reduced.
  • The other benefit is that this code is compiled only once by the JIT and after that it can be run utilizing full power of the CPU.
  • So the million dollar question is when ultimately the native code is getting generated which is also being generated by other language compilers what's the difference between this native code and the one generated by CLR.
  • So the difference is that CLR places some information within this native code which tells this native code to follow the execution model and remain in bounds confined by the CLR.
  • Earlier unmanaged applications never thought of maximizing the overall throughput,they always used to maximize individual applications throughput but managed code always tries to maximize the overall throughput rather then considering a single process.

So this was the first article of this Series which tells very basic things about the CLR.In future articles we will dig deep into the features provided by the CLR.

10 comments :

Anonymous said...

Very well written article about CLR basics.

Thanigainathan said...

Hei ,

Nice article. Gives a very basic and indepth undertanding of .Net .

Thanks ,
Thani

Jef Claes said...

Nice Read. Look forward to the next in line.

Shakti Singh Dulawat said...

This is very nice article , I write a smiler article about it
do not forgot to read
What is Common Language Runtime


Thanks
Shakti
http://www.nextmvp.blogspot.com/

Aashish said...

@all
Thanks for all your valuable comments

suresh said...

Thanks for starting such good series for beginners or even intermediates.......Looking forward for upcoming CLR atricles...

Thanks,
Suresh

Anonymous said...

Nice article. This series would certainly unlesah the myth sourrounding the CLR.

yasi said...

Very good article. Clearing the basic concepts of CLR. Need more description about CRL. Waiting for the upcoming Parts.

Thanks,
Yasir Miraj

Programming360 said...

Great post.

Pritesh said...

Really nice article. It is as easy as to understand any fresher person. Good work.

Post a Comment