I had a question about some of the basic concepts and needed help.
When one runs a managed app then the CLR compiles the managed IL code into
native code. Is this done for the full application or just for the function
called?
I earlier thought that the code of the full application is converted to
native code and then this native code is cached so that for future runs of
this app this step is not needed and the native code can be directly run. But
now I am not sure.
Also, is the native code cached? Is the cache retained between machine
reboots? Can one configure the duration the cache is maintained?
Thanks,
- Manoj
> I had a question about some of the basic concepts and needed help.
>
> When one runs a managed app then the CLR compiles the managed IL code into
> native code. Is this done for the full application or just for the function
> called?
On the desktop framework it occurs the first time any particular method
is called. On the compact framework I *think* it's done a class at a
time, but I can't remember for sure.
> I earlier thought that the code of the full application is converted to
> native code and then this native code is cached so that for future runs of
> this app this step is not needed and the native code can be directly run. But
> now I am not sure.
You can do that with ngen.exe.
> Also, is the native code cached? Is the cache retained between machine
> reboots? Can one configure the duration the cache is maintained?
No, the native code isn't cached unless you use ngen.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Manoj Agarwal - 01 Mar 2006 13:52 GMT
Thanks for the reply Jon.
Its not clear to me what you meant for desktop app- when a method is called
for the first time then is the full app converted to native code or just that
method\class?
Also, I have noticed that if you run a managed app then time taken is more
on the first run and its almost always faster after that. You can write a
Hello World app and run it multiple times to confirm this. This makes me
believe that there might be some caching of the native code which the CLR
does. Your thoughts...
- Manoj
> > I had a question about some of the basic concepts and needed help.
> >
[quoted text clipped - 17 lines]
>
> No, the native code isn't cached unless you use ngen.
Willy Denoyette [MVP] - 01 Mar 2006 17:55 GMT
No, the caching is done by the OS in the file system cache for non ngen'd
images or on XP and higher in the prefetch (disk) cache for native images
(as the ngen'd images).
A non Ngen'd image (just a regular file) is loaded by the OS using normal
file IO, that means that the assembly passes the cache where it stays until
it's removed in favor of other files. When you load a program a second time
(after the first one has gone) and the file data is still in cache, it will
get loaded from the cache, that's why it loads so fast the second time.
An Ngen'd image is loaded differently, here the OS loader loads the code
image in memory (actually it maps the data into the process space) bypassing
the FS cache completely.
However on XP and higher, the OS keeps part of the loaded native image in
the prefetch directory (under \windows) , which it loads as part of the boot
process. That means that programs that have been loaded recently (the
threshold is a couple of days depending on the size and number of images
loaded) are partly preloaded to speed-up the final load process.
Willy.
| Thanks for the reply Jon.
|
[quoted text clipped - 31 lines]
| >
| > No, the native code isn't cached unless you use ngen.
Jon Skeet [C# MVP] - 01 Mar 2006 19:20 GMT
> Thanks for the reply Jon.
>
> Its not clear to me what you meant for desktop app- when a method is called
> for the first time then is the full app converted to native code or just that
> method\class?
Just the method.
> Also, I have noticed that if you run a managed app then time taken is more
> on the first run and its almost always faster after that. You can write a
> Hello World app and run it multiple times to confirm this. This makes me
> believe that there might be some caching of the native code which the CLR
> does. Your thoughts...
You'll see that on plenty of applications. All it means is that the
binaries and libraries required by the app are then in the file system
cache, so don't need to be loaded from the disk.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too