tags:

views:

638

answers:

4

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 how Windows handles COM components and CLR in particular. Hope someone could detail down to how CLR loads in memory and what it means if I get multiple CLR instances listed on running this command:

tasklist /m mscor*

Is it multiple CLRs actually or a single CLR as a COM server for all .NET processes?

A: 

The CLR is actually the component that transforms MSIL into native machine code. That native machine code then runs in its own process for each application.

And in the .NET world a process is closely related to an appdomain. That's why I think this is a good starting point.

fretje
Care to elaborate on the downvote?
fretje
Sure. I don't think the linked article does much to answer the question. Though you're making me feel bad for the downvote, with the changes you made to your answer.
Michael Petrotta
The CLR is more than just the JIT compiler and the CLR DLLs are loaded into the same process as the application.
Brian Rasmussen
+3  A: 

Each process will have its own copy of the CLR as a hosting process. However, since the CLR is really just a couple of DLLs Windows will be able to share the DLLs between processes. For more information see: http://msdn.microsoft.com/en-us/magazine/cc301727.aspx

Brian Rasmussen
A: 

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
    }
}
Shafqat Ahmed
running tasklist /m mscor* on the command prompt does the same thing as the code snipped above.
Gishu
Well, process explorer from sysinternals does it even better and shows the appdomains as well. I thought the question was to determine this programaticallyIf you want to determine the .NET appdomains and threads .. this article is a good starting point http://msdn.microsoft.com/en-us/magazine/cc163900.aspx#S6
Shafqat Ahmed
+1  A: 

A managed exe has an additional CLR header an addition to the Portable Executable (PE format). The OS now is able to determine if the launched exe is a "managed" exe, and hence loads the CLR behind the scenes and gives it control.

  • mscoree.dll is a shim dll (the latest version of this file is always present in the Windows/System32 folder and hence knows how to load current and older versions of the CLR.)
  • mscorwks.dll is the actual implementation of the CLR. You will find multiple versions of this dll if you have multiple versions of the framework installed. The right version of this dll is loaded by the shim dll.

It follows from the above, that each managed executable's process would have its own copy of the CLR (2 Dlls). ManagedExecutable1 may be using CLR v1 where as ManagedExecutable2 may be using CLR v2. They are not shared as of now.
The Garbage collector is part of the CLR and hence is also distinct across processes for managed executables.

Gishu
I had an idea that CLR is a COM Component - which cannot be loaded as seperate copies in different processes. The purpose of my query is to establish and the right fact - "how and in which mode the CLR exists on a machine running .net apps - is it loaded just once by OS and shared to all .net processes as a COM dll or it is something otherwise.
MSIL
It is loaded per process. The CLR is implemented in COM. Multiple processes could be running different versions of the CLR respectively - hence cannot be loaded just once for all .net processes.
Gishu