Java Garbage Collection thread priority

user689842 picture user689842 · Apr 5, 2011 · Viewed 8.4k times · Source

I have been asked in an interview the following question: "What is the default priority of the Garbage Collection thread?" I know we cannot force a GC or change its priority, though I never heard about its default priority. Does anyone know?

Answer

JasCav picture JasCav · Apr 5, 2011

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: