本文介绍了有没有垃圾收集发生在进程级或AppDomain的水平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FullGC normaly暂停所有的线程运行时。有两个应用程序域,每个运行多个线程。当GC运行时,将所有的线程被暂停,或只有一方的AppDomain?

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?

推荐答案

很难回答,最好的办法只是测试它:

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));
    }
}

输出:

In appdomain 'test', collection count = 3, gen = 2
Primary appdomain, collection count = 3, gen = 2

好证据表明,GC影响默认的CLR主机上的所有应用程序域。这让我很吃惊。

Good evidence that a GC affects all AppDomains on the default CLR host. This surprised me.

这篇关于有没有垃圾收集发生在进程级或AppDomain的水平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:37