Ian Boyd
Ian Boyd

Reputation: 256761

When is a .NET assembly unloaded?

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

Upvotes: 5

Views: 287

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500903

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.

Upvotes: 7

Related Questions