FullGC 通常会在运行时暂停所有线程。有两个 AppDomain,每个都运行多个线程。 GC 运行时,是所有线程都暂停,还是仅暂停其中一个 AppDomain 的线程?
最佳答案
很难回答,最好的办法就是测试一下:
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 会影响默认 CLR 主机上的所有 AppDomain。这让我很惊讶。
关于.net - 垃圾收集发生在进程级别还是应用程序域级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15246167/