none
System.OutOfMemoryException at System.Reflection.AssemblyName.nGetFileInformation

    Question

  • Hello,

    Our internal  API regression tests randomly failing with system out of memory exceptions.

    The call stack for all the failed tests are same.

     

    Error: Exception of type 'System.OutOfMemoryException' was thrown. --    at System.Reflection.AssemblyName.nGetFileInformation(String s)
       at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)
       at System.ComponentModel.Composition.Hosting.AssemblyCatalog.LoadAssembly(String codeBase)
       at System.ComponentModel.Composition.Hosting.AssemblyCatalog..ctor(String codeBase, ICompositionElement definitionOrigin)
       at System.ComponentModel.Composition.Hosting.DirectoryCatalog.CreateAssemblyCatalogGuarded(String assemblyFilePath)
       at System.ComponentModel.Composition.Hosting.DirectoryCatalog.Initialize(String path, String searchPattern)
       at System.ComponentModel.Composition.Hosting.DirectoryCatalog..ctor(String path, String searchPattern)
       at qcom.mm.avt.QACT.Controller.PluginImporter.PopulatePluginList() in c:\Jenkins-Slave\workspace\QACT.WIN.7.x_COMPILE_DEV\QACT\Libraries\QACTLib\QACTController\PluginManager\PluginViewManager.cs:line 62

     

    The code snippet, it is pointing is for plugin list collection using MEF. The highlighted line giving exception

     

    public virtual void PopulatePluginList()

          {

              foreach (string strFPPath in m_PPathList)

              {

                  string strPluginPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                  strPluginPath = Path.Combine(strPluginPath, strFPPath);

                  DirectoryCatalog dirCatalog;

                  try

                  {

                      dirCatalog = newDirectoryCatalog(strPluginPath, "*.dll");//exception thrown here

                      //Adds all the parts found in all assemblies in

                      //the same directory as the executing program

                      m_catalog.Catalogs.Add(dirCatalog);

                  }

                  catch (DirectoryNotFoundException ex)

                  {

                      Logger.Instance.Log(LogMsgTypes.Error, "PLM", ex.Message);

                  }

              }

     

              //Create the CompositionContainer with the parts in the catalog

              m_container = new CompositionContainer(m_catalog);

              //Fill the imports of this object

              m_container.ComposeParts(this);

          }

     

    Everytime this is giving exception in construction of DirectoryCatalog construction, where we are loading around 45 files.

     

    I have modified the solution to avoid DirectoryCatalog and used plain assembly catalog.

     

          public virtual void PopulatePluginList()

          {

              foreach (string strFPPath in m_PPathList)

              {

                  string strPluginPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                  strPluginPath = Path.Combine(strPluginPath, strFPPath);

                  try

                  {

                      string[] files = Directory.GetFiles(strPluginPath, "*.dll", SearchOption.TopDirectoryOnly);

                      AggregateCatalog aggCat = new AggregateCatalog();

                      foreach (string file in files)

                      {

                          AssemblyCatalog assemblyCatalog = null;

                          assemblyCatalog = CreateAssemblyCatalogGuarded(file);//This will valid parts only

                          if (assemblyCatalog != null)

                          {

                              aggCat.Catalogs.Add(assemblyCatalog);

                          }

                      }

                      m_catalog.Catalogs.Add(aggCat);

                  }

                  catch (DirectoryNotFoundException ex)

                  {

                      Logger.Instance.Log(LogMsgTypes.Error, "PLM", ex.Message);

                  }

              }

              m_container = new CompositionContainer(m_catalog);

              //Fill the imports of this object

              m_container.ComposeParts(this);

          }

     

      private AssemblyCatalog CreateAssemblyCatalogGuarded(string assemblyFilePath)

          {

              Exception exception = null;

              try

              {

                  return new AssemblyCatalog(assemblyFilePath); //exception thrown here

              }

              catch (FileNotFoundException ex)

              {

                  exception = ex;

              }

              catch (FileLoadException ex)

              {

                  exception = ex;

              }

              catch (BadImageFormatException ex)

              {

                  exception = ex;

              }

              catch (ReflectionTypeLoadException ex)

              {

                  exception = ex;

              }

              return null;

          }

    But this is also gives same exception.

     

    System.Reflection.AssemblyName.nGetFileInformation(String s)

       at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)

       at System.ComponentModel.Composition.Hosting.AssemblyCatalog.LoadAssembly(String codeBase)

       at System.ComponentModel.Composition.Hosting.AssemblyCatalog..ctor(String codeBase)

       at qcom.mm.avt.QACT.Controller.PluginImporter.CreateAssemblyCatalogGuarded(String assemblyFilePath) in c:\Jenkins-

     

     

    this issue happens randomly once in 5 iterations. We have about 45 files/dlls to load as plugins. The machine it runs has enough RAM storage.

    Searched online resources but not helping.. requesting for help.

    Friday, March 31, 2017 11:18 AM

All replies

  • Not seen this before but I'm curious if one of your DLLs is not an assembly but a native DLL and therefore the code is failing. Have you ensure that every DLL in your directory is an assembly. If there are some that aren't then you'll need to change your filter first.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Friday, March 31, 2017 3:19 PM
    Moderator
  • Thanks Michael for your response.

    There are native dlls also present the plugin directory but this is handled properly, we are handling this with NULL check.

    creation of Assemblycatalog object will give an exception in case of Native DLL, where we return the NULL and we are not going to add it.

    This problem is only occurring randomly( 1 out of 5 times) for the same inputs, especially using with MSTest in build machines this is happening could not reproduce when debugging in local machine.

    curious any known issues with MSTest + MEF usage.

    Thanks

    Pavan

    Saturday, April 1, 2017 9:07 AM
  • There are issues using MEF with MSTest (you can google to hear about them) but I haven't heard anything about OOM issues. Sort of sounds like MEF might be using up the memory in your test process. I'd be curious to know if the test(s) that fail run fine if you run them alone. If so it might be an issue with MEF and test process. Unfortunately I don't use MEF so I don't know what else could be wrong here.
    Saturday, April 1, 2017 6:59 PM
    Moderator
  • Hi Thankyou,Standalone testing also has same behavior.
    Sunday, April 2, 2017 6:20 PM
  • I guess I'm still not convinced the problem isn't with loading the native DLLs. Can you eliminate them from the catalog and see if it works? Getting an assembly name only requires reading the metadata but if it is a pure PE file I could see it allocating the wrong amount of memory.
    Monday, April 3, 2017 1:13 AM
    Moderator
  • I have added the logs to print DLL name when this is giving exception. I will keep you posted with results.
    Tuesday, April 4, 2017 6:18 AM
  • X86 or X64. 

    The maximum addressable memory for your X86 computer is 2Gb.

     


    Success
    Cor

    Tuesday, April 4, 2017 6:24 AM