AppDomain 相当于 .NET 代码的 Process 吗?
我必须调用一些写得不好的第 3 方 COM 组件,这些组件存在内存泄漏,并在长时间运行的进程中使用单线程单元 [STA]。
我知道单独的进程将是实现它的好方法,我可以偶尔从长时间运行的进程中重新启动它。
可以用AppDomain代替吗? 如果适当标记的话,AppDomain 线程是否为 STA 线程? 它有自己的 COM 对象内存吗? 卸载AppDomain是否相当于杀死进程?
I have to call some badly written 3rd party COM components that have memory leaks and uses Single Threaded Apartment [STA] within a long running process.
I know separate process will be nice way to implement it and I can restart it occasionally from the long running process.
Can AppDomain be used instead? Is AppDomain thread a STA thread if marked appropiately? Does it have its own memory for COM objects? Is unloading the AppDomain is equivalent of killing the process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AppDomain 不提供与进程相同程度的隔离。 事实上,如果您担心第 3 方组件状态不佳,则存在风险,它会导致您的 .NET 应用程序崩溃。
如果卸载时正在执行非托管代码,则无法卸载 AppDomain,因此您可能很难控制 AppDomain 中的第 3 方代码。 请参阅 http://msdn.microsoft.com/en-us /library/system.appdomain.unload.aspx
即使仅对于托管代码,AppDomain 也不提供强大的沙箱解决方案。 例如,如果加载的代码产生任何线程,那么在出现未处理的异常的情况下,这些线程将停止整个进程。 这个问题有更多信息: .NET - 实现“捕获所有异常处理程序”的最佳方法是什么。
据我所知,在 .NET 应用程序中托管代码的最佳选择是像 IIS 和 SQL Server 一样实现您自己的 CLR 主机进程。
An AppDomain does not provide the same degree of isolation as a process does. In fact if you're worried that the 3rd party component is not in good shape there's a risk, that it will take down your .NET application.
An AppDomain cannot be unloaded if unmanaged code is executing at the time of unload, so you may have a hard time controlling your 3rd party code in an AppDomain. See http://msdn.microsoft.com/en-us/library/system.appdomain.unload.aspx
Even for managed code only, an AppDomain does not provide a robust sandbox solution. E.g. if the loaded code spawns any threads these will take down the entire process in case of unhandled exceptions. This question has a bit more info: .NET - What's the best way to implement a "catch all exceptions handler".
As far as I am aware the best option for hosting code like that in a .NET application is to implement your own CLR host process like IIS and SQL Server does.
AppDomain(应用程序域)是一个隔离的环境,其中应用程序执行。
SO 可能感兴趣的问题:
什么是 .Net 应用程序域?< /a>
我不会自称是 AppDomains 领域的专家,但我相当确定COM 对象泄漏内存(即非托管内存)不会通过卸载 AppDomain 来释放。 也许更熟悉这一点的人可以发表评论。
正如 Brian 指出的那样,“……在 .NET Framework 2.0 版域中,不能保证卸载,因为可能无法终止正在执行的线程。”
An AppDomain (application domain), is an isolated environment where applications execute.
SO Questions that might be of interest:
What is a .Net app domain?
I don’t understand AppDomains
I won't profess to be an expert in the area of AppDomains, but I am fairly sure that a COM object leaking memory (i.e. unmanaged memory) will not be freed by unloading the AppDomain. Perhaps someone more familiar with this could comment.
As Brian pointed out, "... in the .NET Framework version 2.0 domain is not guaranteed to unload, because it might not be possible to terminate executing threads. "