3

Either local or global (GAC / .NET) DLL references, how expensive they are?

For sure we should never reference things we don't use, but for curiosity I ask: Would it be a big performance concern referencing the whole .NET framework?

A similar more practical question would be: Is it worth to combine similar namespaces in projects to minimize the DLL files that need to be referenced (as long as I have to use every code in those DLLs in any case)?

Martin Braun
  • 10,906
  • 9
  • 64
  • 105
  • Unless your application is highly performance sensitive (i.e. you're counting microseconds) or you're severely memory constrained, then the performance cost of another DLL will be pretty minor. You're better off organizing your projects logically, without worrying about the cost of another DLL. Only if testing shows that particular DLL references are impacting performance, then consider addressing those references that the profiler says are a problem. – Jim Mischel Feb 16 '16 at 16:30
  • Hmm, you never reference anything from the GAC. You are forcing the compiler to read the metadata of every reference you add. File I/O is one of the more expensive things a compiler has to do, but on a modern machine with an SSD that tends to be hidden pretty well. The disdainful look from your team members is surely the more practical problem. – Hans Passant Feb 16 '16 at 16:36

1 Answers1

7

A reference is loaded only when you execute a method, which uses a type from the referenced .dll. So even if you reference the whole .NET framework, it will not be loaded automatically.

class Program
{
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad;
        Console.WriteLine("=====Working with URI...=====");
        WorkWithUri();

        Console.WriteLine("=====Working with XML...=====");
        WorkWithXml();
    }

    private static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
    {
        Console.WriteLine(args.LoadedAssembly.FullName + " has been loaded.");
    }

    private static void WorkWithUri()
    {
        var uri = new Uri("c:\\");
    }

    private static void WorkWithXml()
    {
        XDocument xml = new XDocument();
    }
}

And the output:

=====Working with URI...=====
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 has been loaded.
=====Working with XML...=====
System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 has been loaded.
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 has been loaded.
System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 has been loaded.
Press any key to continue . . .
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65