Number of CLR and GC instances running on a machine?

前端 未结 4 1744
逝去的感伤
逝去的感伤 2020-12-29 10:59

I create 2 .NET applications and run them on a machine - how many CLR\'s and gc\'s will be there?

In addition: I would like to have some background information on ho

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 11:20

    I would say that you can easily count processes that run or load the CLR by inspecting the loaded dlls. But I am not sure if you will be able to count the number of application domains running. But I do not think that is your objective.

    There is only one heap per process and also one GC, which suspends all managed threads during collection. So you could iterate through the processes and check if mscorlib is loaded, if so you can assume that that is running a .NET CLR and a GC. I am sure that there should be better ways to determine if a process has CLR hosted, please check the CLR API as well.

    Please try Jeffrey Richter's book CLR via C# to have a closer understanding.

    The code below iterates .NET processes

    // Import these namespaces
    using System.Diagnostics;
    using System.ComponentModel;
    
    // Here is the code
    Process[] prcs = Process.GetProcesses();
    foreach (Process prc in prcs)
    {
        try
        {
            foreach (ProcessModule pm in prc.Modules)
            {
                if (pm.ModuleName.Contains("mscorlib"))
                {
                    Console.WriteLine(prc.ProcessName);
                }
            }
        }
        catch (Win32Exception exWin)
        {
            // Cannot detemine process modules ... some will deny access
        }
    }
    

提交回复
热议问题