Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a .NET assembly unloaded?

Tags:

.net

clr

We know that a .NET assembly is loaded at the last possible moment - when you enter a method that references a something in the foreign assembly.

When is an assembly unloaded? Is it when there are no longer any references to any classes/resources in the assembly? Is it never?

Imagine the class in the foreign assembly has a private static. The static is initialized when first needed. Presumably that static will no longer contain a value when the static variable is removed from memory.

When is an unreferenced static class removed from memory?

When is a .NET assembly unloaded?

Bonus Reading

  • When are referenced Assemblies loaded?
  • Delay Loading .Net Assemblies
like image 463
Ian Boyd Avatar asked May 20 '14 15:05

Ian Boyd


People also ask

How do you unload an assembly?

To unload an assembly in the . NET Framework, you must unload all of the application domains that contain it. To unload an application domain, use the AppDomain. Unload method.

What is a .NET assembly file?

. NET defines a binary file format, assembly, that is used to fully describe and contain . NET programs. Assemblies are used for the programs themselves as well as any dependent libraries.

Where does .NET look for assemblies?

The runtime always begins probing in the application's base, which can be either a URL or the application's root directory on a computer. If the referenced assembly is not found in the application base and no culture information is provided, the runtime searches any subdirectories with the assembly name.

What is AppDomain in C#?

The AppDomain class implements a set of events that enable applications to respond when an assembly is loaded, when an application domain will be unloaded, or when an unhandled exception is thrown.


1 Answers

It's only unloaded when the AppDomain that it's loaded into is unloaded, e.g. as part of a web app recycling.

Any classes within the assembly stay loaded while the assembly is loaded, so therefore they stick around until the AppDomain is unloaded too.

like image 180
Jon Skeet Avatar answered Oct 19 '22 14:10

Jon Skeet