TOP KEK
TOP KEK

Reputation: 2651

Does GC stop all app threads in .NET?

I was reading and article about optimizing .Net app performance. The authors state

it shows what the main thread did during all those GCs. Most of the time (97.3%) it was Waiting. This means that GC took place on some other thread (obviously on the FileProcessing thread) and the main thread had to wait until the GCs were finished.

As far as I know .Net GC is stop-the-world collector. Thus, if your Main thread is waiting, that means ALL other threads must be waiting as well. Except GC thread itself.

Is the article outdated or I'm getting it wrong?

Upvotes: 3

Views: 2229

Answers (2)

Prolog
Prolog

Reputation: 3374

Taken from offical documentation about Garbage Collection in .NET:

Before a garbage collection starts, all managed threads are suspended except for the thread that triggered the garbage collection.

The following illustration shows a thread that triggers a garbage collection and causes the other threads to be suspended:

enter image description here

Upvotes: 0

TOP KEK
TOP KEK

Reputation: 2651

The reason is misunderstanding between Background GC which happens in a separate thread and Blocking GC which runs on the same thread as the one who triggered GC.

https://jetbrains.com/help/profiler/CLR_Activity.html

Upvotes: 3

Related Questions