.NET Garbage Collector - what is its priority in the thread?

I found some great articles ( Maoni , Richter # 1 , Richter # 2 ) that give a lot of details regarding GC theory and practice, but I can’t find anything that determines how GC thread priority is set.

The closest I found is one that claims the Finalizer thread "works asynchronously with the application and with high priority."

I always thought it was a low priority thread, but it reads more and more about it, which seems wrong (since the GC has to block all your other threads, and you don't want your application to depend on the low priority thread in order to resume it in a timely manner )

Does anyone know exactly what the actual priority is?

+5
garbage-collection
source share
5 answers

In the CLR via C #, Richter explains that:

Special high-priority CLR thread designed to call Finalize methods

(see the heading "Completion of the Interior" of chapter 20)

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

  • Generation 0 is full
  • Call GC.Collect
  • Windows reports low memory.
  • CLR unloads AppDomain
  • CLR shuts down

... which assumes that the only thread created by the garbage collector is the only "high priority" stream finalizer.

Edit: He then continues to the “Parallel Collection” to explain that:

In a multiprocessor system running a version of the workstation of the execution engine, the garbage collector has an additional background thread to collect objects at the same time during application startup. [...] The garbage collector has a normal priority background thread that marks unreachable objects.

+9
source share

GC thread runs with normal priority. The finalizer runs with the highest priority.

You can see this by turning on the Debug "Thread" window and crashing anywhere in the managed application. All threads are listed (although they are not named), with their priorities. It takes a little to decrypt what is, but there will be an additional stream of "Normal" and "Highest" priority, which corresponds to the flow of GC and Finalizer.

+5
source share

The flow performed by the GC depends on which version of GC your application is using.

The answer to this question is contained in a blog post that I wrote: How to determine which garbage collector works

+1
source share

garbage collector works with lower priority than critical threads

0
source share

The priority of the GC thread is an implementation detail, they now work with normal priority, however the finalizer thread works as a high priority and also has a timeout.

0
source share

All Articles