


How Can I Efficiently Unload an Assembly Loaded with Assembly.LoadFrom()?
Jan 02, 2025 pm 08:36 PMUnloading Assembly Loaded with Assembly.LoadFrom()
To test the time taken by GetTypes() after loading a DLL, let's explore the process of unloading and reloading the DLL.
Unloading the Assembly
When an assembly is loaded using Assembly.LoadFrom(), it is added to the AppDomain's list of assemblies. To unload it, there is no explicit method in Assembly or AppDomain for unloading. However, you can create a new AppDomain to load the assembly and then unload the AppDomain to release the resources.
Reload and Time Comparison
To reload the DLL, follow these steps:
- Create a new AppDomain with a different name.
- Load the assembly into the new AppDomain using AppDomain.Load().
- Get the time taken by GetTypes() for the reloaded assembly.
- Unload the AppDomain to release resources.
Example:
string file = "path/to/assembly.dll"; // First AppDomain AppDomain dom1 = AppDomain.CreateDomain("domain1"); Assembly assem1 = dom1.Load(file); Stopwatch sw1 = Stopwatch.StartNew(); var types1 = assem1.GetTypes(); sw1.Stop(); double time1 = sw1.Elapsed.TotalMilliseconds; AppDomain.Unload(dom1); // Second AppDomain AppDomain dom2 = AppDomain.CreateDomain("domain2"); Assembly assem2 = dom2.Load(file); Stopwatch sw2 = Stopwatch.StartNew(); var types2 = assem2.GetTypes(); sw2.Stop(); double time2 = sw2.Elapsed.TotalMilliseconds; AppDomain.Unload(dom2); Console.WriteLine($"First Load: {time1} milliseconds"); Console.WriteLine($"Second Load: {time2} milliseconds");
This example creates two AppDomains and loads the DLL into each. It then measures the time taken by GetTypes() for both instances. The difference in timings can indicate the overhead incurred by reloading the DLL.
Explicit Garbage Collection
assem = null is not sufficient to release resources allocated to an Assembly. Explicitly calling the garbage collector will not help either, as AppDomain-specific resources are not managed by it. Using a new AppDomain is the recommended approach for unloading assemblies and releasing their associated resources.
The above is the detailed content of How Can I Efficiently Unload an Assembly Loaded with Assembly.LoadFrom()?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the types of values returned by c language functions? What determines the return value?

C language function format letter case conversion steps

What are the definitions and calling rules of c language functions and what are the

Where is the return value of the c language function stored in memory?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

How does the C Standard Template Library (STL) work?
