S. Valmont
S. Valmont

Reputation: 1031

Number of .NET Garbage Collector Threads

How many of these run at any given time? Is it one for the entire Framework, or one per .NET managed process? When physical memory is in abundance, is it correct to assume there are no active GC threads?

Upvotes: 3

Views: 2234

Answers (3)

driis
driis

Reputation: 164341

There are two modes of .NET garbage collection, server and workstation. For workstation, you will have one garbage collector thread per .NET process. If you are running server garbage collection mode, you will have one garbage collector thread per process and processor. So if you have a .NET process running as server on a 4 core CPU, you will have 4 garbage collector threads.

Also, for .NET 4, a new "background garbage collection" mode exists. It will collect items in generation 0 and 1 concurrently.

Garbage collection might happen even if you have plenty physical memory. .NET will allocate memory for its heap in blocks. In general terms, something like this happens: When new memory is needed, .NET will try to find a block of memory on it's heap that will fit the required memory chunk. If unsuccessful, a GC will run to try to collect any unneeded objects. Only if that does not free enough memory, a new block of memory will be allocated for the heap from the underlying OS.

Details are in the MSDN article, Fundamentals of Garbage Collection.

All this being said, I highly agree with one of the other answers on this question: Do not make any assumptions on the GC. It should be irrelevant to your application how memory is allocated and collected.

Upvotes: 7

Deepansh Gupta
Deepansh Gupta

Reputation: 593

I think if you can explain your requirement, it would be helpful is answering what you are looking for. Ideally you should not make any assumptions about the GC.

Upvotes: 1

foxy
foxy

Reputation: 7672

Though I don't know how many threads the GC uses, a new instance of the .NET runtime (framework) is instantiated for each process. Each process's .NET runtime is independent of another. Therefore, there is a different GC instance (and thus thread) for each process.

The GC will continue to collect and clean up memory, even in an environment with abundant available physical memory. Though I don't know for sure (no source code or documentation), the GC may clean up objects less aggressively than it might in a low-memory environment.

Upvotes: 2

Related Questions