Java Garbage Collection thread priority Java Garbage Collection thread priority multithreading multithreading

Java Garbage Collection thread priority


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

With that said, if you read Sun literature on garbage collection, just running GC as a low-priority thread is not quite right. In actuality, the GC may not be just a single thread. Instead, the GC runs when memory is low (although, determining when memory is low is still probably done in a background thread - maybe someone else can clarify this).

Here are some good links for reading on GC:


I would say the correct answer to this question is "if you think you need to worry about the thread priority of the garbage collector, you're probably doing something wrong".

Remember that thread priority isn't necessarily directly related to how much CPU time a process gets. It varies from system to system, but on Windows, thread priority is essentially used to determine the ORDER in which threads that are waiting to run are scheduled on to the available CPUs, so that high-priority threads can pre-empt low-priority threads, assuming that both threads are actually competing for CPU. There's no real rule to "give CPUs with lower priority less CPU time". (For what it's worth, on Linux, there is a little bit more of a direct relationship between thread priorities (nice values) and allocated CPU time.)

With thread priorities used as they are in Windows, for a background thread like a garbage collector, a more appropriate solution may-- perhaps paradoxically-- to give it a HIGH priority and then control the proportion of CPU usage by some other means (essentially, deliberately sleeping for appropriate proportions of time or waiting for appropriate signals). Specifically, the high priority is appropriate for a background thread that doesn't need to do anything most of the time, but when it does need to do something, it needs to do it as soon as possible.

I actually haven't looked at what thread priorities are used, if any, by particular garbage collection algorithms. But my point is that the situation is somewhat complex and it seems odd to base any assumptions about the behaviour of the garbage collector on thread priorities.

Those interested more in thread priorities may like to look at some measurements of the effect of thread priorities that I took-- admittedly a couple of years ago now and this material could do with being updated.

Update: by coincidence, a talk by Cliff Click was posted on YouTube yesterday. About 35 minutes in, he mentions precisely this point, that certain JIT and GC threads need to run high priority so that they don't get starved.


It is low priority thread (not sure about exact priority). The point here is to avoid GC to slow down ordinary thread where possible. I would say it has lower than normal priority :)