14

During discussion with my collegue, I got a doubt that the garbage collector in .net works system wide or application wide.

Means if every application having its own GC then will it effect system performance?

I am little bit confused about that.

Pragmateek
  • 13,174
  • 9
  • 74
  • 108
Syed
  • 931
  • 13
  • 28
  • It is just like any native process, they too get their own chunk of virtual memory. Windows 3.11 was the last version of Windows that didn't prevent apps from destabilizing each other. It crashed a lot. – Hans Passant Aug 08 '11 at 12:56

4 Answers4

25

Each process has its own managed heap, which will be collected separately.

There's no system-wide heap, so there can be no system-wide GC.

(If you're running multiple CLRs in the same process, they'd each have their own GC too. That's a pretty rare situation though.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I marked as correct answer. Please clarify me will it not effect the overall system performance? – Syed Aug 08 '11 at 12:55
  • @Syed: You haven't marked anything as the correct answer yet. But it's not clear what you mean by "it will not effect the overall system performance" - the GC is doing work, so if you have lots of processes each consuming resources by performing GC, then that will obviously affect the overall system performance, the same as anything else. – Jon Skeet Aug 08 '11 at 12:56
  • I think the GC does take some account of free memory system wide when deciding how often to do a collection. – Ian Ringrose Aug 08 '11 at 12:59
  • @Jon Skeet: Thanks, I am not able to mark as correct answer. It is asking me to wait ten minutes. – Syed Aug 08 '11 at 13:00
  • 1
    Just to clarify too, a process is the overall Win32 structure that manages the containment of an executable, however many .NET applications may run inside a process each in their own AppDomain (think of them as smaller processes specifically for .NET apps). You can read more about AppDomains [here](http://msdn.microsoft.com/en-us/library/system.appdomain.aspx) – Paul Carroll Aug 08 '11 at 13:21
  • Joh, may I ask here, as another question was closed without answers as not real. So basically heap is shared between threads and domains, and there is no way to separate heaps(SOH, LOH) within one processm without loading another CLR? – Johnny_D Nov 24 '14 at 10:20
  • @Johnny_D: No, please *don't* add additional questions as comment threads. That's not what comments are for. – Jon Skeet Nov 24 '14 at 10:25
7

There is a Garbage Collector per process in the Workstation\Server version of the .Net runtime. GC introduces a CPU overhead per managed process.

Whether this has an impact on system performance depends on how many managed processes you have and if they are spending a lot of time collecting garbage. You can analyse how much time your process is spending collecting garbage by inspecting performance counter "% Time Spent In GC".

Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
5

Every application has its own heap and .NET runtime instance, and so it also has its own garbage collector thread.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
2

Have a look at this page (http://msdn.microsoft.com/en-us/library/ee787088.aspx), especially under the section The Managed Heap. That should answer your questions.