I am trying to get matured to write some notes(short) on code refactoring. Though i can steal it from other popular blogs (as usual :) ), i want to put forward only those points that i am able to adapt and relate with the code that i write.
For now, these are the useful links in the same lines.
http://www.codinghorror.com/blog/2006/05/code-smells.html
http://www.soberit.hut.fi/mmantyla/BadCodeSmellsTaxonomy.htm
Friday, April 23, 2010
Monday, April 19, 2010
CLR, Process, Application, AppDomain, Thread - .net
The idea behind this post is to present a relation between process, application(windows),appdomain, thread. All these are inter-related but the relation is not obvious all the time (atleast not to me, unless i read CLR via C#).
What was confusing to me was loading of CLR inside a process. If there is one instance of CLR running in a single process, how would it be possible to run multiple windows applications?
Well, the question is just to form a base behind this post :).
When we run a window application it runs in a separate process altogether. Now, the runtime knows that it is to be run under a managed environment (CLR) and reads the .net version in which the application was tested, and load the appropriate version of CLR into the same process.
On top of the CLR lies the memory for domain neutral assemblies (mscorlib), and the loader heap for the same. And then, the runtime loads the default appdomain in the same process.
We can load more than one AppDomain inside a process. The default appdomain is the starting project, and then it may load one or more appdomains if needed.
[To remember, Whole purpose of appdomain is to provide Isolation. It is a logical container for a set of assemblies]
The story beyond AppDomain is rather simple, we can run one or more threads inside a AppDomain. Then there is some concept of main thread and worker threads which is interesting but i am not going to talk about :).
Best Wishes.
PS: This post doesn;t takes web application into account. In web app we have a concept of worker process, which opens multiple threads for different user sessions.
What was confusing to me was loading of CLR inside a process. If there is one instance of CLR running in a single process, how would it be possible to run multiple windows applications?
Well, the question is just to form a base behind this post :).
When we run a window application it runs in a separate process altogether. Now, the runtime knows that it is to be run under a managed environment (CLR) and reads the .net version in which the application was tested, and load the appropriate version of CLR into the same process.
On top of the CLR lies the memory for domain neutral assemblies (mscorlib), and the loader heap for the same. And then, the runtime loads the default appdomain in the same process.
We can load more than one AppDomain inside a process. The default appdomain is the starting project, and then it may load one or more appdomains if needed.
[To remember, Whole purpose of appdomain is to provide Isolation. It is a logical container for a set of assemblies]
The story beyond AppDomain is rather simple, we can run one or more threads inside a AppDomain. Then there is some concept of main thread and worker threads which is interesting but i am not going to talk about :).
Best Wishes.
PS: This post doesn;t takes web application into account. In web app we have a concept of worker process, which opens multiple threads for different user sessions.
Part II.
Inspired by a post in StackOverFlow. To Summarize,
1. CLR is loaded PER Process. If we run two windows app which are developed using .net runtime, then the process of each of these app will load CLR.
2. Some part of the CLR is shared across processes also. So we second application is started then it will take little less time because some part of the CLR is already loaded in the OS memory.
This is more interesting information at MSDN
So, before any managed code could be run by a process there has to be a running CLR in THAT process. How CLR is loaded in that process? That is something i had to figure out too.
The Windows operating system does not provide support for running a CLR application. That support is provided by a CLR host. A CLR host is an application that is responsible for loading the CLR into a process, creating application domains within the process, and executing user code within the application domains.
Examples of hosts that ship with the .NET Framework include:
1. ASP.NET. An ISAPI filter that ships with ASP.NET loads the CLR and does the initialization necessary to handle web requests.
2. Internet Explorer. A MIME filter hooks into Internet Explorer versions 5.01 and higher to execute managed controls referenced from HTML pages.
3. Shell Executables. When a managed application is launched from a shell, a small piece of unmanaged code loads the CLR and transitions control of the application to the CLR.
So, in case of Windows application it is the shell executable (unmanaged code) that takes control when and exe(.net) is being run by the user. The shell executable identifies the CLR version which was used to compile the exe, finds it in the machine, and hosts the CLR in the running process and then transfers the control the running CLR instance. Amazing!.
Subscribe to:
Posts (Atom)