Ответ 1
Трудно ответить, лучше всего просто проверить это:
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));
}
}
Вывод:
In appdomain 'test', collection count = 3, gen = 2
Primary appdomain, collection count = 3, gen = 2
Хорошее доказательство того, что GC влияет на все AppDomains на хосте CLR по умолчанию. Это меня удивило.