问题
If I had a 1000 asp.net websites each with 30 DLL's in their /bin folders.
Therefore 30,000 DLL's.
Would the websites / web server / machine run faster if I registered one set of the DLL's into the Global Assembly Cache and each site used the DLL's in the GAC?
e.g. would the websites collectively use less memory?
回答1:
Even though assemblies are in the GAC they will still be loaded from there into memory separately to provide isolation. In other words, just because assemblies are in the GAC doesn't mean that those copies are shared across AppDomains. I believe that mscorlib (and possibly a few of the BCL assemblies) can be shared across AppDomains but any assemblies you or I write will not be.
This is a good thing however: consider the implications of the Cache type being shared across AppDomains.
回答2:
Actually this would be much faster, particular if they are NGEN'd as well. If you NGEN without putting them in the Global Assembly Cache then you will actually slow things down because the CLR will need to perform verification of the assembly to ensure that it matches the native image. The CLR skips this check for GAC'd assemblies and will simply load and use the native image.
There are also memory benefits to NGEN'd assemblies because they can share code pages.
You might also consider trying to optimize the base addresses of the DLL's because if they're all using the default, then Windows needs to rebase 30,000 times!
Here's a great article on performance benefits of NGEN.
http://msdn.microsoft.com/en-us/magazine/cc163610.aspx
回答3:
Actually, the accepted answer from @AndrewHare is incorrect. Placing assemblies in the GAC DOES help reduce memory usage.
This is confirmed by Jeffrey Richter (from Wintellect who helped design the CLR with the .NET team) in his book CLR via C#:
Installing assemblies into the GAC offers several benefits. The GAC enables many applications to share assemblies, reducing physical memory usage on the whole....
It is also confirmed by Tess Ferrandez (Memory and Performance Guru from Microsoft's - https://blogs.msdn.microsoft.com/tess/2006/04/12/asp-net-memory-you-use-the-same-dll-in-multiple-applications-is-it-really-necessary-to-load-it-multiple-times).
Wherever possible strong name and install to the global assembly cache (GAC) any assemblies that are used by more than one ASP.NET application. This will reduce memory consumption.
I've also confirmed this myself by testing (WinDbg, Task Manager, and ProcExplorer) on a x64 WebAPI service as an example. You'd see that the Private Working Set is lower with the GAC'd app. In the case of NGen, you would again see the Private Working Set decreased. However, the Page Faults are also greatly reduced in the NGen'd app compared to the baseline (almost by half in my test). I saw no difference in Page Faults between the GAC'd app and non-GAC'd app.
Note that the best solution in some cases is to combine NGen and the GAC by installing NGen'd assemblies into the GAC. This optimizes memory usage between apps that share assemblies as well as providing a performance boost at application startup!
来源:https://stackoverflow.com/questions/945273/speed-increase-by-using-the-global-assembly-cache