使用反射加载程序集时的内存管理
所有,
我正在创建一个使用复合模式的组件。核心组件使用 XMl 元数据来定义组合(部件)。在运行时,核心组件将使用反射将部件组件加载到内存中并调用方法(例如 IPart.execute 方法)。
现在我的问题是
1)当我处置对象时,使用反射加载的程序集占用的(动态)内存是否会被卸载。
2)如果它不卸载并释放内存,有什么方法可以将其从内存中删除。
这个问题的原因是,我正在构建的组件将是我的企业应用程序的业务层的核心,可以大量定制。
谢谢 阿尔伯特·阿鲁尔·普拉卡什
All,
I am creating a component that uses composite pattern. The core component uses an XMl Meta data to define the composites (parts). at run time, the core component would use reflection to load the part assembly into the memory and call methods (e.g IPart.execute method).
Now my question is
1) will the (dynamic) memory that is occupied by the assembly that is loaded using reflection will be unloaded when i dispose the object or not.
2) if it does not unload and free the memory, is there is any way i can remove it from memory.
The reason of this question is, the component which i am building will be the core of my Business layer of an enterprise application which can be customized heavily.
Thanks
Albert Arul Prakash
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我见过人们通过反射在另一个应用程序域中加载其他库(我们可以将这些库称为“插件”)。例如,请参阅这篇文章: http://adrianvintu.com/blogengine/post/Unloadable- 这样你就可以免受“邪恶”插件
的侵害,并且可以在这样的应用程序域中管理内存(当应用程序域卸载时,内存也会被释放)
I've seen people load additional libraries via reflection (we could call these libraries "plugins") in another appdomain. For instance, see this article: http://adrianvintu.com/blogengine/post/Unloadable-plugins.aspx
This way you are protected from "evil" plugins as well as memory can be managed in such appdomain (when the appdomain unloads, the memory is freed up as well)
插件/模块中有两件事会消耗内存。总体来说,这些是代码(将程序集加载到进程空间中会消耗内存)和对象(创建某事物的实例会消耗内存)。
就内存而言,对
IDisposable
调用Dispose
不会对对象执行任何操作。它可能会释放对象使用的资源(例如,如果您关闭文件,它将摆脱打开的文件句柄),但它不会释放对象本身。IDisposable
不是一个神奇的内存释放函数 - 它只是接口上的一个方法,让对象知道它应该摆脱它拥有的资源。要释放对象本身,您必须删除对其的所有引用(可能将它们设置为 null,或者让它们从程序堆栈中消失),并且垃圾收集器最终必须运行以回收该内存。
如果您只关心 GUI 和文件句柄等资源,请确保调用
Dispose
。您应该始终这样做:)如果您担心对象内存,只需让 GC 完成其工作即可。也别去纠缠它。让它自己运行。
如果您担心代码内存,则必须卸载代码所在的
AppDomain
。如果这是您的默认AppDomain
,则在不退出程序的情况下无法卸载它。相反,您应该将该插件加载到您在运行时创建的子AppDomain
中。然后,您可以通过卸载子AppDomain
将代码从进程空间中取出。有关如何使用的信息,请参阅天真的答案子
AppDomain
。There are two things that can consume memory in a plugin/module. Grossly speaking, these are code (loading the assembly into your process space consumes memory), and objects (creating an instance of something consumes memory).
Calling
Dispose
onIDisposable
doesn't do anything to the object as far as memory is concerned. It might free resources that the object uses (e.g. if you close a file, it will get rid of open file handles), but it won't free the object itself.IDisposable
isn't a magic memory-freeing function - it is just a method on an interface that lets an object know that it should get rid of resources that it owns.To free the object itself, you must get rid of all references to it (possibly set them to null, or let them fall off your program's stack), and the garbage collector must eventually run to reclaim that memory.
If you are only concerned about resources like GUI and file handles, make sure you call
Dispose
. You should always do this :)If you are concerned about object memory, just let the GC do its work. Don't pester it to, either. Let it run on its own.
If you are concerned about code memory, you must unload the
AppDomain
that the code is in. If this is your defaultAppDomain
then you can't unload it without quitting your program. Instead, you should load that plugin in a subAppDomain
that you created at runtime. You can then get the code out of your process space by unloading the subAppDomain
.See naivists' answer for information on how to use a sub
AppDomain
.