Java garbage collection thread priority

In an interview, I was asked the following question: "What is the default priority for the garbage collection stream?" I know that we cannot force GC or change its priority, although I have never heard of its default priority. Somebody knows?

+8
java garbage-collection multithreading thread-priority
source share
5 answers

Probably the answer the interviewer was looking for is that the GC is in a low priority background process. The reason for this is that launching the GC is expensive, but it is not (usually) a critical process, so it should be done only when the system has time to do this, and not interrupt critical tasks. (A similar idea exists in real-time systems - perform non-essential processes in the background task and all critical processes in the foreground - they will all have a higher priority than the background task.)

With that said, if you read Sun's garbage collection literature, just starting the GC as a low-priority stream is not quite right. In fact, a GC may not be just a single thread. Instead, the GC starts when the memory is low (although determining when the memory is low is still probably done in the background thread - maybe someone else can figure this out).

Here are some good links to read on the GC:

+8
source share

I would say that the correct answer to this question is "if you think that you need to worry about the priority of the garbage collector stream, you probably have something wrong."

Remember that thread priority is not necessarily directly related to how much processor time a process receives. It varies from system to system, but on Windows, the priority of a thread is essentially used to determine the ORDER, in which threads waiting to start are scheduled for available processors, so high priority threads can prevent low priority threads, assuming that both threads are actually compete for the processor. There is no real rule to give processors with lower priority less processor time. (For what, on Linux, there is a slightly more direct relationship between thread priorities (good values) and CPU time allocated.)

When using thread priorities used by Windows for a background thread such as a garbage collector, a more appropriate solution might, perhaps paradoxically, be to give it a high priority and then control the percentage of CPU usage by others means (essentially intentionally sleep for the appropriate proportions time or wait for the corresponding signals). In particular, a high priority is suitable for a background thread that should not do anything most of the time, but when it needs to do something, it should do it as soon as possible.

I really did not look at what thread priorities are used, if any, by specific garbage collection algorithms. But I think the situation is somewhat complicated, and it seems strange to base any assumptions about the behavior of the garbage collector in thread priorities.

Those who are more interested in thread priorities can look at some dimensions of the impact of thread priorities that I took - admittedly, a couple of years ago, and this stuff can do with the update.

Update: Coincidentally, talk by Cliff Click was published yesterday on YouTube. After about 35 minutes, he mentions this very moment that certain JIT and GC threads must have high priority so that they are not depleted.

+4
source share

Perhaps the question was directed at the actual implementation of the JVM. As you can read in the online links, there are several ways to implement the garbage collector, and it can change from version to version. This is why everyone tells you not to rely on GC behavior. This may work differently on another JVM.

+2
source share

This is a low priority thread (not sure of the exact priority). The point here is to prevent the GC from slowing down a regular thread where possible. I would say that it has a lower priority than usual :)

+1
source share

At least in Java RTS, the priority of the garbage collection stream (s) can be adjusted according to need. Priority tuning (and thread scheduling in general) also differs from several processors than with one.

At the moment, I am assuming a configuration with multiple CPUs, as this is (by far) the most common. I also only talk about the default scheduler - other schedulers can do things differently. Your threads are basically divided into two classes: critical and non-critical priority (you can also have โ€œno heap in real timeโ€ threads that are higher than others, but since they cannot / cannot use the heap, they have little to do with GC ) Garbage collection threads usually start with a lower priority than any of them, but can be increased to a higher priority than your non-critical threads when necessary. GC thread priority always stays lower than your critical real-time streams.

I was a little vague in the distinction between "critical" and "non-critical" priorities for some reason: open to customization. You can choose which priorities of your threads can be uploaded by GC and what not. The goal is that critical flows receive a real-time hard response, and non-critical flows get a real-time soft response. It is up to you to decide / configure which streams fall into which category.

+1
source share

All Articles