问题描述
我需要检查加载 dll 后运行 GetTypes() 的时间量.代码如下.
I need to check the time amount to run GetTypes() after loading the dll. The code is as follows.
Assembly assem = Assembly.LoadFrom(file);
sw = Stopwatch.StartNew();
var types1 = assem.GetTypes();
sw.Stop();
double time1 = sw.Elapsed.TotalMilliseconds;
我想卸载并重新加载 dll 以检查再次运行 GetTypes() 所花费的时间.
I'd like to unload and reload the dll to check the time to spend in running GetTypes() again.
- 如何卸载?
assem = null够好吗? - 有没有明确的方法来调用垃圾收集器来回收分配给 assem 的资源?
推荐答案
不幸的是,一旦程序集被加载,您就无法卸载它.但是您可以卸载 AppDomain.您可以做的是创建一个新的 AppDomain (AppDomain.CreateDomain(...) ),将程序集加载到此 appdomain 中以使用它,然后在需要时卸载 AppDomain.卸载 AppDomain 时,将卸载所有已加载的程序集.(请参阅参考)
Unfortunately you can not unload an assembly once it is loaded. But you can unload an AppDomain. What you can do is to create a new AppDomain (AppDomain.CreateDomain(...) ), load the assembly into this appdomain to work with it, and then unload the AppDomain when needed. When unloading the AppDomain, all assemblies that have been loaded will be unloaded. (See reference)
要调用垃圾收集器,可以使用
To call the garbage collector, you can use
GC.Collect(); // collects all unused memory
GC.WaitForPendingFinalizers(); // wait until GC has finished its work
GC.Collect();
GC 在后台线程中调用终结器,这就是为什么您必须等待并再次调用 Collect() 以确保您删除了所有内容.
GC calls the finalizers in a background thread, that's why you have to wait and call Collect() again to make sure you deleted everything.
这篇关于卸载使用 Assembly.LoadFrom() 加载的程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)