Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Garbage Collector - what is its thread priority?

I have found some great articles (Maoni, Richter #1, Richter #2) giving many details as to the theory and practice of the GC, yet I cannot find anything that states how the GC's thread priority is set.

The closest I've found is this one that states that the Finalizer thread "runs asynchronously to the application and at a high priority."

I always thought that it was a "low-priority" thread, but reading more and more about it that seems to be wrong (since the GC has to block all your other threads, and you don't want to have your app depend on a low-priority thread in order to resume in a timely fashion).

Does anybody know for sure know what the actual priority is supposed to be?

like image 293
Erich Mirabal Avatar asked May 06 '09 17:05

Erich Mirabal


People also ask

What is the priority of the garbage collector thread of?

The garbage collector runs in a low priority both synchronously and asynchronously depending on the situation and the system on which Java is running.

Is garbage collector high priority thread?

The garbage collector has a normal priority background thread that marks unreachable objects. Save this answer.

How does .NET handle garbage collection internally?

. NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

How does .NET garbage collection work?

Garbage collection is an automatic process, when object is created then it will be placed in the Generation 0. The garbage collection uses an algorithm which checks the objects in the generation, the objects life time get over then it will be removed from the memory.


2 Answers

In CLR via C#, Richter explains that:

A special high-priority CLR thread is dedicated to calling Finalize methods

(see the "Finalization Internals" heading of chapter 20)

This is the only context in which he talks about a garbage collector thread. A little earlier in the chapter, he explains that garbage collection is started in response to one of the following events:

  • Generation 0 is full
  • Call to GC.Collect
  • Windows is reporting low memory conditions
  • The CLR is unloading an AppDomain
  • The CLR is shutting down

...which suggests that the only thread created by the garbage collector is this single, "high-priority" finalizer thread.

Edit: He then goes on, in "Concurrent Collection", to explain that:

On a multiprocessor system running the workstation version of the execution engine, the garbage collector has an additional background thread to collect objects concurrently while the application runs. [...] The garbage collector has a normal priority background thread that marks unreachable objects.

like image 127
Tim Robinson Avatar answered Oct 12 '22 22:10

Tim Robinson


The GC thread runs at a normal priority. The finalizer thread runs at "Highest" priority.

You can see this by turning on the Debug "Thread" window, and breaking anywhere in a managed application. The threads are all listed (although they're not named), with their priorities. It takes a bit to decipher which is which, but there will be an extra "Normal" and "Highest" priority thread, which correspond to the GC and the Finalizer thread.

like image 23
Reed Copsey Avatar answered Oct 12 '22 22:10

Reed Copsey