FullGC normaly pauses all Threads while running. Having two AppDomains, each running several threads. When GC runs, will all threads be paused, or only those of either one AppDomain?
2 Answers
Hard to answer, best thing to do is just test it:
using System;
using System.Reflection;
public class Program : MarshalByRefObject {
static void Main(string[] args) {
var dummy1 = new object();
var dom = AppDomain.CreateDomain("test");
var obj = (Program)dom.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Program).FullName);
obj.Test();
Console.WriteLine("Primary appdomain, collection count = {0}, gen = {1}",
GC.CollectionCount(0), GC.GetGeneration(dummy1));
Console.ReadKey();
}
public void Test() {
var dummy2 = new object();
for (int test = 0; test < 3; ++test) {
GC.Collect();
GC.WaitForPendingFinalizers();
}
Console.WriteLine("In appdomain '{0}', collection count = {1}, gen = {2}",
AppDomain.CurrentDomain.FriendlyName, GC.CollectionCount(0),
GC.GetGeneration(dummy2));
}
}
Output:
In appdomain 'test', collection count = 3, gen = 2
Primary appdomain, collection count = 3, gen = 2
Good evidence that a GC affects all AppDomains on the default CLR host. This surprised me.

- 922,412
- 146
- 1,693
- 2,536
-
Are you sure each AppDomain has its own heap? That contradicts Brian Rasmussen's answer here: http://stackoverflow.com/questions/574708/what-is-appdomain and Aghilas Yakoub's answer here: http://stackoverflow.com/questions/12219815/appdomains-and-gc-heap – Tim Goodman Mar 06 '13 at 14:07
-
"AppDomains are part of the same process and thus actually share the same managed heap". Doesn't that contradict what you said? See also my edit to my comment, Aghilas says "You have heap for an process, and the app domains share this heap." Unless I'm misunderstanding, it seems like at least one answer (yours or theirs) is in need of correction. – Tim Goodman Mar 06 '13 at 14:13
-
1Okay, I'll tone that down. They are logically distinct since the object handle points to an AppDomain specific data structure. And thus needs to be marshaled across domains. But there sure is evidence from the test that the GC heap itself is shared. – Hans Passant Mar 06 '13 at 14:22
-
@TimGoodman Note that .NET manages a lot of different managed heaps at the same time, even in the same `AppDomain`. This is especially pronounced when using server GC, where every thread can have its own heap. However, the GC still has to go through all of them, because they can have cross-references. – Luaan Mar 30 '15 at 15:55
From this thread here: Is the garbage collector in .net system-wide or application-wide?, it occurs on the process level. All threads in that process will be paused but not across multiple processes.
One or multiple app domains can exist within a process but app domains are not shared between processes. Per: http://blogs.msdn.com/b/tess/archive/2008/08/19/questions-on-application-domains-application-pools-and-unhandled-exceptions.aspx,
"all appdomains in the process share the same GC."
Accordingly, GC should affect all app domains when a GC is triggered.
However, a CPU performance hit can occur with too many processes spending time doing GC which can negatively affect the performance of other processes not involved in a GC.
This link also explains the fundamentals of GC too:

- 11,743
- 10
- 52
- 81

- 4,142
- 2
- 31
- 40