Is AppDomain equivalent to a Process for .NET code?

前端 未结 2 1869
不思量自难忘°
不思量自难忘° 2021-02-02 17:25

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

2条回答
  •  猫巷女王i
    2021-02-02 17:38

    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:

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

提交回复
热议问题