AppDomain 相当于 .NET 代码的 Process 吗?

发布于 2024-07-30 12:42:34 字数 223 浏览 4 评论 0原文

我必须调用一些写得不好的第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肤浅与狂妄 2024-08-06 12:42:34

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.

¢蛋碎的人ぎ生 2024-08-06 12:42:34

AppDomain(应用程序域)是一个隔离的环境,其中应用程序执行。

它们有助于提供隔离,
卸载和安全边界
执行托管代码。

  • 使用应用程序域来隔离可能导致进程崩溃的任务。
    如果 AppDomain 的状态是
    执行任务变得不稳定,
    AppDomain 无需卸载即可卸载
    影响进程。 这是
    当进程必须运行时很重要
    长时间不重新启动。 你
    还可以使用应用程序域
    隔离不应共享的任务
    数据。

  • 如果程序集加载到默认应用程序域中,则它不能
    从内存中卸载
    进程正在运行。 但是,如果您
    打开第二个应用程序域
    加载并执行程序集,
    组件被卸载时
    应用程序域已卸载。 使用
    这种技术可以最大限度地减少工作
    一组长时间运行的进程
    偶尔使用大型 DLL。

可以运行多个应用程序域
在单一流程中; 然而,有
之间不是一对一的相关性
应用程序域和线程。
多个线程可以属于一个
应用程序域,并且同时给定
线程不限于单个
应用程序域,在任何给定时间,
一个线程在单个执行
应用程序域。

SO 可能感兴趣的问题:

我不会自称是 AppDomains 领域的专家,但我相当确定COM 对象泄漏内存(即非托管内存)不会通过卸载 AppDomain 来释放。 也许更熟悉这一点的人可以发表评论。

正如 Brian 指出的那样,“……在 .NET Framework 2.0 版域中,不能保证卸载,因为可能无法终止正在执行的线程。”

An AppDomain (application domain), is an isolated environment where applications execute.

They help provide isolation,
unloading, and security boundaries for
executing managed code.

  • Use application domains to isolate tasks that might bring down a process.
    If the state of the AppDomain that's
    executing a task becomes unstable, the
    AppDomain can be unloaded without
    affecting the process. This is
    important when a process must run for
    long periods without restarting. You
    can also use application domains to
    isolate tasks that should not share
    data.

  • If an assembly is loaded into the default application domain, it cannot
    be unloaded from memory while the
    process is running. However, if you
    open a second application domain to
    load and execute the assembly, the
    assembly is unloaded when that
    application domain is unloaded. Use
    this technique to minimize the working
    set of long-running processes that
    occasionally use large DLLs.

Multiple application domains can run
in a single process; however, there is
not a one-to-one correlation between
application domains and threads.
Several threads can belong to a single
application domain, and while a given
thread is not confined to a single
application domain, at any given time,
a thread executes in a single
application domain.

SO Questions that might be of interest:

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. "

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文