none
NGEN.EXE Install assembly RRS feed

  • Question

  • If I have  a.dll that I locate in directory c:\test\a.dll.

    I successfully run an ngen install of the dll and can see it clearly in an NGEN display, as well as the root section

    If I copy the dll to another folder .. example c:\test2\a.dll and load the a.dll from this folder, does the machine use the native image cache from my original ngen install. I am guessing it does not as there is no ngen root referernce to c:\test2\a.dll in my ngen display. I have searched the iternet for this answer and am hoping someone here has an opinion, or better yet, an answer.

     

    Thanks ! 

    Tuesday, September 14, 2010 5:58 PM

Answers

  • Hi,

    Thanks for your post.

    Here is some quotation from here (search for "Assembly and the GAC").

    "In order to get the optimal performance from NGen, all assemblies in the application need to be strong-named and installed into the Global Assembly Cache (GAC) prior to compilation. When the loader attempts to load a pure MSIL assembly from the GAC and discovers a matching NGen image, it directly loads the NGen image. In that scenario, not even a single page of the MSIL assembly needs to be fetched into memory."

    So, supposing you install your a.dll into GAC and NGEN it as quoted above, it will always be the NGEN-ed version that is used as long as the strong name is unchanged.

    The following sample could demonstrate this behavior.

    I wrote a MainApp.exe and Lib.dll as below:

    Lib.dll

    using System;
    
    namespace Lib
    {
      public class Class1
      {
        public static void method()
        {
          Console.WriteLine("I am the old one!");
        }
      }
    }
    
    

    MainApp.exe

    using Lib;
    
    namespace MainApp
    {
      class Program
      {
        static void Main(string[] args)
        {
          Class1.method();
        }
      }
    }
    
    

    After I installed the Lib.dll into the GAC and NGEN it. I change the Lib.dll's implementation as below, but I didn't change the strong name of Lib.dll:

    Lib.dll

    using System;
    
    namespace Lib
    {
      public class Class1
      {
        public static void method()
        {
          Console.WriteLine("I am the new one!");
        }
      }
    }
    
    

    Then I run the MainApp.exe, the output is still like this:

    I am the old one!
    

    So, as long as the strong name is the same, the GAC version will be in effect, NGEN is just for performance.

     

    Some references:

    1. The Performance Benefits of NGen

    2. How the Runtime Locates Assemblies


    Please mark the right answer at the right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Monday, September 27, 2010 3:36 AM
    Wednesday, September 15, 2010 8:42 AM