I want to measure the time that .NET runtime takes to load individual assemblies.
To start, I've prepared the following console app:
public static void Main(string[] args)
{
var timer = Stopwatch.StartNew();
var assembly = Assembly.LoadFrom(@"<path to an assembly>");
timer.Stop();
Console.WriteLine($"Assembly: {assembly.FullName}\nLoad time: {timer.Elapsed.TotalMilliseconds}ms");
}
I noticed that the measured time is significantly higher the first time I run the app for an assembly.
For example the first time I've tried loading HdrHistogram.dll I got the following result:
Load time: 51,8103ms
But then, on consecutive runs the results are much lower, for example:
Load time: 3,1396ms
When I change the requested assembly to any other assembly, I see the same pattern: high value for first run and lower values for consecutive runs.
When I restart my PC, the measured loading time goes back to the initial value for the first run and falls again for every consecutive run.
Restarting the machine is actually the only way of getting the original measurement. I've tried recompiling the app, compiling and running another app and restarting console. The measured values always stay low, even for not related .NET apps.
I observe the same behavior for Assembly.LoadFrom and Assembly.Load methods and both strongly named and regular assemblies.
It looks like there is some form of assembly cache in the OS memory but I could not find any information on that and I failed to locate any .NET related services running in the background.
I am interesed in measuring the initial loading time so my question is:
- what is the exact mechanism behind the observed behavior
- and how can I overcome it for my measurements other than restarting PC?