FullGC通常会在运行时暂停所有线程。具有两个AppDomain,每个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/