-2

Below is the quote from a book:

When a thread allocates an object that pushes generation 0 over its budget, the GC first suspends all threads and then determines which generations to collect. If the garbage collector needs to collect generation 0 or 1, it proceeds as normal.

Let's say we have three concurrent threads, threa 1, threa 2 and thead 3, at some point of time, thread 3 allocate a new object to heap, which causes the budget of generation 0 to be full, GC needs to run.

My quesitons are:

Q1-which thread runs the GC?

Q2-If it is thread 3 to run GC, my understanding is, when thread 3 runs GC, somwhow thread 3's context needs to be saved first, then it stop thread 1 and thread 2 then do the GC, when it finishes GC, thread 3 needs to restore its prevous thread context to execute what it prevously execute before GC happened. Is my understanding correct?

Q3-If my understanding to Q2 is correct, saving and restoring thread context is expensive work, why not CLR create a new dedicate thread to run GC instead of using existing thread to run GC?

P.S. I know CLR uses a dedicate thread to collect generation 2 objects, this question is about genration 0 and generation 1 collection, why not also use a dedicate thread to collect genration 0 and generation 1 objects?

amjad
  • 3,048
  • 1
  • 11
  • 42
  • You are aware that thread context switches happen *all of the time* in normal operation, right? Why do you think that this one particular context switch would be more expensive than any other? E.g. on my system at the moment there are ~3400 threads, and trust me, they don't each have a dedicated processor thread. – Damien_The_Unbeliever Apr 07 '21 at 07:21
  • 2
    Does this answer your question? [.NET Garbage Collector in a separate Thread?](https://stackoverflow.com/questions/32639298/net-garbage-collector-in-a-separate-thread) and [Is it OK to run GC.Collect in a background thread?](https://stackoverflow.com/questions/28761689/is-it-ok-to-run-gc-collect-in-a-background-thread) and [new Thread() and Garbage Collection](https://stackoverflow.com/questions/9011831/new-thread-and-garbage-collection) –  May 22 '21 at 10:42

1 Answers1

2

Background garbage collection, which is the standard garbage collection from 4.0ish runs on dedicated thread(s).

Miscrosoft's Background garbage collection article states:

In background garbage collection (GC), ephemeral generations (0 and 1) are collected as needed while the collection of generation 2 is in progress. Background garbage collection is performed on one or more dedicated threads, depending on whether it's background or server GC, and applies only to generation 2 collections.

(...)

The following illustration shows background workstation garbage collection performed on a separate, dedicated thread:

Background workstation garbage collection

The following illustration shows background server garbage collection performed on separate, dedicated threads:

Background server garbage collection

tymtam
  • 24,269
  • 5
  • 71
  • 101
  • I know CLR uses a dedicate thread to collect generation 2 objects, this question is about genration 0 and generation 1 collection, why not also use a dedicate thread to collect genration 0 and generation 1 objects? – amjad Apr 07 '21 at 04:08
  • It does run on dedicated threads in the server server (vs. workstation) garbage collection. – tymtam Apr 07 '21 at 04:22
  • the link you post says: "Background garbage collection is performed on one or more dedicated threads, depending on whether it's background or server GC, and applies only to generation 2 collections." so dedicate thread only apply generation2 collections, not generation 0 or generation 1 – amjad Apr 07 '21 at 04:32