Tuesday, June 16, 2009

Executing Managed Code !!

Dev Palmistry

Assemblies provide a way to package modules containing MSIL and metadata into units for deployment. The goal of writing code is not to package and deploy it, however; it's to run it. The final section of this chapter looks at the most important aspects of running managed code.
Assemblies are loaded into memory only when needed

Loading Assemblies
When an application built using the .NET Framework is executed, the assemblies that make up that application must be found and loaded into memory. Assemblies aren't loaded until they're needed, so if an application never calls any methods in a particular assembly, that assembly won't be loaded. (In fact, it need not even be present on the machine where the application is running.) Before any code in an assembly can be loaded, however, it must be found. How is this done?

The answer is not simple. In fact, the process the CLR uses to find assemblies is too complex to describe completely here. The broad outlines of the process are fairly straightforward, however. First, the CLR determines what version of a particular assembly it's looking for. By default, it will look only for the exact version specified for this assembly in the manifest of the assembly from which the call originated. This default can be changed by settings in various configuration files, so the CLR examines these files before it commences its search.

The CLR follows well-defined but involved rules to locate an assembly
Once it has determined exactly which version it needs, the CLR checks whether the desired assembly is already loaded. If it is, the search is over; this loaded version will be used. If the desired assembly is not already loaded, the CLR will begin searching in various places to find it. The first place the CLR looks is usually the global assembly cache (GAC), a special directory intended to hold assemblies that are used by more than one application. Installing assemblies in this global assembly cache requires a process slightly more complex than just copying the assembly, and the cache can contain only assemblies with strong names.

The CLR looks first in the global assembly cache
If the assembly it's hunting for isn't in the global assembly cache, the CLR continues its search by checking for a codebase element in one of the configuration files for this application. If one is found, the CLR looks in the location this element specifies, such as a directory, for the desired assembly. Finding the right assembly in this location means the search is over, and this assembly will be loaded and used. Even if the location pointed to by a codebase element does not contain the desired assembly, however, the search is nevertheless over. A codebase element is meant to specify exactly where the assembly can be found. If the assembly is not at that location, something has gone wrong, the CLR gives up, and the attempt to load the new assembly fails.

The CLR can next look in the location referenced by a codebase element
If there is no codebase element, however, the CLR will begin its last-ditch search for the desired assembly, a process called probing, in what's known as the application base. This can be either the root directory in which the application is installed or a URL, perhaps on some other machine. (It's worth pointing out that the CLR does not assume that all necessary assemblies for an application are installed on the same machine; they can also be located and installed across an internal network or the Internet.) If the elusive assembly isn't found here, the CLR continues searching in several other directories based on the name of the assembly, its culture, and more.

If no codebase element exists, the CLR searches in other places

Despite the apparent complexity of this process, this description is not complete. There are other alternatives and even more options. For developers working with the .NET Framework, it's probably worth spending some time understanding this process in detail. Putting in the effort up front is likely to save time later when applications don't behave as expected.

Compiling MSIL
A compiler that produces managed code always generates MSIL. Yet MSIL can't be executed by any real processor. Before it can be run, MSIL code must be compiled yet again into native code that targets the processor on which it will execute. Two options exist for doing this: MSIL code can be compiled one method at a time during execution, or it can be compiled into native code all at once before an assembly is executed. This section describes both of these approaches.

JIT Compilation
The most common way to compile MSIL into native code is to let the CLR load an assembly and then compile each method the first time that method is invoked. Because each method is compiled only when it's first called, the process is called just-in-time (JIT) compilation.
MSIL code is typically JIT compiled before it's executed

7 comments:

Shama said...

I surely will have read this article with lot more concentration..may be i can seek your guidance as well, if u have the time!!

Thanks a lot for your comment on my poem on "Pitajee" blog!

http://lalitlekh.blogspot.com

http://aajtakyahantak-thelightbyalonelypath.com

http://kavitasbyshama.blogspot.com

U will find other links to my blogs, on these very blogs..have divided them content wise!
Will surely look forward to seeing you more often!

Swatantra said...

Hey

Your Plamistry blog is amazing!! Great passion you have!!

mark rai said...

very nice....one of the greatest post....thanks

आकांक्षा~Akanksha said...

Well! Nice Article...keep it up.U r most welcome on my blog Shabd-Shikhar.

Dhiraj Shah said...

धन्यवाद comment के लिये & nice post

shama said...

Thanks for ur lovely comment on my "sansmaran" blog!

http://shamasansmaran.blogspot.com

http://lalitlekh-thelightbyalonelypath.blogspot.com

http://shama-baagwaanee.blogspot.com

U r an youngster..I wld really want u to read articles like,"Gazab qanoon"...related to police reforms..& some of the redundant, 150 yrs old laws..!I will truly appreciate ur valuable comments...

Wonder,if u wld like to read, a few of my articles in English..u just have to click on labels,like,"moon","Little girl",'praknster'...once u reach there, u can read, the excerpts of my long pending,English book, "From distant horizons..!"

Suman said...

nice