Table of Contents
Unloading Assembly Loaded with Assembly.LoadFrom()
Unloading the Assembly
Reload and Time Comparison
Example:
Explicit Garbage Collection
Home Backend Development C++ How Can I Efficiently Unload an Assembly Loaded with Assembly.LoadFrom()?

How Can I Efficiently Unload an Assembly Loaded with Assembly.LoadFrom()?

Jan 02, 2025 pm 08:36 PM

How Can I Efficiently Unload an Assembly Loaded with Assembly.LoadFrom()?

Unloading 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:

  1. Create a new AppDomain with a different name.
  2. Load the assembly into the new AppDomain using AppDomain.Load().
  3. Get the time taken by GetTypes() for the reloaded assembly.
  4. 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");
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

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

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

C language function format letter case conversion steps

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

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

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

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 do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

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

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

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

See all articles