Tuning Stop-The-World garbage collection


STW garbage collection means that the JVM stops all application threads to carry out garbage collection through the "GCThread#*" threads. You will want to minimize this time as these are short, but inevitable total halts. It is reported on the VM summary page in JConsole as "G1 Young generation".

You will be able to do some simple math from that page, determining frequency and CPU use by STW GC. The STW time reported there will be higher what you see from thread monitoring (e.g. top). This is because
thread monitoring only counts time spent in the GC threads, but for the JVM, GC time is the time when the application is stopped, ranging from the start of the first GC thread to the last one finishing.

You can minimize this difference (and you will want to) by running STW GC at highest priority. Example for Linux:

while read pid _
do
    sudo renice -20 $pid &> /dev/null
done < <(ps hH -e -o 'tid comm' | grep "GC Th")

Next thing to tune is the number of GC threads. By default the JVM starts as many GC threads as it sees processors available (unless > 8). The GC threads operate more efficiently (using less total CPU time) when they have a large chunk of memory to work on. So will you first want to allocate enough heap memory. If you allocate too much, intervals between garbage collections will be very long, but also the time of the STW pause will be longer than desired. Setting heap memory to a value that results in G1 Young generation collections once per second is enough.

In the following cases you will want to reduce the number of GC threads, saving CPU because now each one gets more memory assigned : You set the number of GC threads using:

<-- Back