Using System.Diagnostics.Process to look for NGEN’ed images


Just like System.Diagnostics.Stopwatch is fabulously useful for a quick and dirty test that you can use to measure performance while prototyping something, so is System.Diagnostics.Process very useful when you need to make sure that your test is using the correct binaries.


It isn’t unusual that you will change binaries back and forth, say to measure a baseline and the effects of your changes, but you want to make sure that you haven’t messed up your environment.


To do that, the Process.Modules property gives you access to all the libraries you have (in the form of a collecton of ProcessModule). I will typically run something similar to this at the end of a case to make sure that the test is referencing the right binaries, then comment it out to avoid having it interfere with my test.


Console.WriteLine(“Loaded modules (* indicates proable ngen image):”);
using (Process process = Process.GetCurrentProcess())
{
    foreach(ProcessModule module in process.Modules)
    {
        bool looksLikeNativeImage = module.ModuleName.Contains(“.ni.”);
        if (looksLikeNativeImage)
        {
            Console.Write(“* “);
        }
        else
        {
            Console.Write(”  “);
        }
        Console.WriteLine(module.ModuleName);
    }
}


This sample uses a very simple heuristic to determine whether a given module is an NGEN’ed image – the “.ni.” string will typically be included.


Another thing to try is changing the last call to Console.WriteLine(module.ModuleName) to Console.WriteLine(module.FileName). This prints out the full path of the module, and is useful if you want to know what directory is it that binaries are being loaded from.


Enjoy!

Comments (0)

Skip to main content