1

Is there

  • one Garbage Collector for an entire system
  • one instance of a garbage collector for each user that is logged in
  • one garbage collector for each running .NET application

Or is it none of the above (please explain)?

Scott Dorman
  • 42,236
  • 12
  • 79
  • 110
user62572
  • 1,388
  • 2
  • 15
  • 24

2 Answers2

6

There is one GC thread per .NET process and therefore one Heap per process. However, the objects are mapped to the individual AppDomains in order to provide process isolation benefits. While there can be more than one AppDomain within a process, by default there is only one per process.

Terminal Services:

What this means is that for a typical deployment of an application (called MyApp.exe) in a Terminal Services environment, the distinct instances of the .NET application that users are running will each have independent heaps and active memory management. More importantly, the Terminal Services sessions themselves represent a memory boundary of sorts given that sessions are unloaded when the user logs off (http://msdn.microsoft.com/en-us/library/aa383496(VS.85).aspx).

The following illustrates how MyApp.exe would be loaded for each Terminal Services session, and it explains some settings that could impact memory availability and performance within a session: http://blogs.technet.com/askperf/archive/2007/07/24/sessions-desktops-and-windows-stations.aspx

Deep Details:

Please refer to Jon Skeet's response here for more detail: What is the scope of finalizer thread - per application domain or per process?

Esoteric Details:

Additionally, the following article explains how Microsoft is now allowing side-by-side CLR instances so that multiple versions of the CLR can be run at the same time. This applies to Silverlight specficially: http://msdn.microsoft.com/en-us/magazine/cc721609.aspx

Also, it appears that the this feature is going to be supported for all future versions of the CLR: http://blogs.msdn.com/davbr/archive/2008/11/10/new-stuff-in-profiling-api-for-upcoming-clr-4-0.aspx

valiano
  • 16,433
  • 7
  • 64
  • 79
  • I'm 99% sure you are incorrect. There is one GC thread per CLR instance and hence on GC per CLR instance. – JaredPar Feb 10 '09 at 00:25
  • I had a sneaky suspicion that I did not explain it correctly, so I did some more research. Am I still off base? – EnocNRoll - AnandaGopal Pardue Feb 10 '09 at 00:38
  • You're still slightly incorrect because of the possibility of multiple CLR instances per process. It's a pretty far off corner case today though. But who knows how popular it will be in the future – JaredPar Feb 10 '09 at 00:41
  • Hm, are you referring to the CLR 4.0, as in the section "In-process side-by-side CLR instances" from http://blogs.msdn.com/davbr/archive/2008/11/10/new-stuff-in-profiling-api-for-upcoming-clr-4-0.aspx ? – EnocNRoll - AnandaGopal Pardue Feb 10 '09 at 00:47
  • It actually predates CLR 4.0. I believe it started with Silverlight 2.0 – JaredPar Feb 10 '09 at 00:51
  • Hm, that's interesting and quite esoteric indeed. The following link describes this still as support for side-by-side CLRs (Desktop and CoreCLR), but they are distinct: http://msdn.microsoft.com/en-us/magazine/cc721609.aspx – EnocNRoll - AnandaGopal Pardue Feb 10 '09 at 00:58
  • Definitely escoteric, but I'm sure for someone, this is a daily pain point – JaredPar Feb 10 '09 at 01:01
  • I have updated the answer to reflect this. I appreciate your watchful eye on this one. I don't want to be wrong, even the least little bit. I want thorough info that can be referenced in the future. – EnocNRoll - AnandaGopal Pardue Feb 10 '09 at 01:07
  • IMHO, Half the fun of posting here is being wrong. Otherwise I wouldn't ever be learning anything :). – JaredPar Feb 10 '09 at 01:09
  • Don't know where you are in NC, but if you are at http://www.trinug.org/ on Wednesday, please introduce yourself. – JaredPar Feb 10 '09 at 01:11
  • That's what I love about this site, and gaining a deeper (and more accurate) understanding is what motivates me. I'm in Winston-Salem. I have not had the opportunity to attend the Triangle user group, but I have considered making the trek. – EnocNRoll - AnandaGopal Pardue Feb 10 '09 at 01:19
  • This is my first meeting so I can't give it a strong enough recomendation to warrant a trip from Winston-Salem ;) – JaredPar Feb 10 '09 at 01:26
  • 1
    Note that there is nothing to stop them changing this, indeed a common approach nowadays is the use of multiple eden (gen 0) areas, one per thread and allow the GC of that to occur inline within that thread as needed. There is still one stop the world GC thread but there's nothing enforcing that. – ShuggyCoUk Feb 11 '09 at 08:54
3

There is on garbage collector per CLR instance in a process. It is possible for multiple instances of the CLR to be running in the same process but that is a pretty rare scenario. In general, there is one CLR per process.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454