Difference between -XX:+UseParallelGC and -XX:+UseParNewGC

JavaJvm Arguments

Java Problem Overview


They are algorithms for the young generation garbage collection.

The second one (UseParNewGC) gets activated automatically with the concurrent tenured generation garbage collection (see Java Concurrent and Parallel GC) but, is there a difference between the two parallel algorithms?

Java Solutions


Solution 1 - Java

After a lot of searching, the best explanation I've found is from Java Performance Tuning website in Question of the month: 1.4.1 Garbage collection algorithms, January 29th, 2003

> Young generation garbage collection algorithms > > The (original) copying collector (Enabled by default). When this collector kicks in, all application threads are stopped, and the copying collection proceeds using one thread (which means only one CPU even if on a multi-CPU machine). This is known as a stop-the-world collection, because basically the JVM pauses everything else until the collection is completed. > > The parallel copying collector (Enabled using -XX:+UseParNewGC). Like the original copying collector, this is a stop-the-world collector. However this collector parallelizes the copying collection over multiple threads, which is more efficient than the original single-thread copying collector for multi-CPU machines (though not for single-CPU machines). This algorithm potentially speeds up young generation collection by a factor equal to the number of CPUs available, when compared to the original singly-threaded copying collector. > > The parallel scavenge collector (Enabled using -XX:UseParallelGC). This is like the previous parallel copying collector, but the algorithm is tuned for gigabyte heaps (over 10GB) on multi-CPU machines. This collection algorithm is designed to maximize throughput while minimizing pauses. It has an optional adaptive tuning policy which will automatically resize heap spaces. If you use this collector, you can only use the the original mark-sweep collector in the old generation (i.e. the newer old generation concurrent collector cannot work with this young generation collector).

From this information, it seems the main difference (apart from CMS cooperation) is that UseParallelGC supports ergonomics while UseParNewGC doesn't.

Solution 2 - Java

Parallel GC

  • XX:+UseParallelGC Use parallel garbage collection for scavenges. (Introduced in 1.4.1)
  • XX:+UseParallelOldGC Use parallel garbage collection for the full collections. Enabling this option automatically sets -XX:+UseParallelGC. (Introduced in 5.0 update 6.)

UseParNewGC

> UseParNewGC > A parallel version of the young generation copying collector is used > with the concurrent collector (i.e. if -XX:+ UseConcMarkSweepGC is > used on the command line then the flag UseParNewGC is also set to true > if it is not otherwise explicitly set on the command line).

Perhaps the easiest way to understand was combinations of garbage collection algorithms made by Alexey Ragozin

<table border="1" style="width:100%">
  <tr>
    <td align="center">Young collector</td>
    <td align="center">Old collector</td>
    <td align="center">JVM option</td>
  </tr>
  <tr>
    <td>Serial (DefNew)</td>
    <td>Serial Mark-Sweep-Compact</td>
    <td>-XX:+UseSerialGC</td>
  </tr>
  <tr>
    <td>Parallel scavenge (PSYoungGen)</td>
    <td>Serial Mark-Sweep-Compact (PSOldGen)</td>
    <td>-XX:+UseParallelGC</td>
  </tr>
  <tr>
    <td>Parallel scavenge (PSYoungGen)</td>
    <td>Parallel Mark-Sweep-Compact (ParOldGen)</td>
    <td>-XX:+UseParallelOldGC</td>
  </tr>
  <tr>
    <td>Serial (DefNew)</td>
    <td>Concurrent Mark Sweep</td>
    <td>
      <p>-XX:+UseConcMarkSweepGC</p>
      <p>-XX:-UseParNewGC</p>
    </td>
  </tr>
  <tr>
    <td>Parallel (ParNew)</td>
    <td>Concurrent Mark Sweep</td>
    <td>
      <p>-XX:+UseConcMarkSweepGC</p>
      <p>-XX:+UseParNewGC</p>
    </td>
  </tr>
  <tr>
    <td colspan="2">G1</td>
    <td>-XX:+UseG1GC</td>
  </tr>
</table>

Conclusion:

  1. Apply -XX:+UseParallelGC when you require parallel collection method over YOUNG generation ONLY, (but still) use serial-mark-sweep method as OLD generation collection
  2. Apply -XX:+UseParallelOldGC when you require parallel collection method over YOUNG generation (automatically sets -XX:+UseParallelGC) AND OLD generation collection
  3. Apply -XX:+UseParNewGC & -XX:+UseConcMarkSweepGC when you require parallel collection method over YOUNG generation AND require CMS method as your collection over OLD generation memory
  4. You can't apply -XX:+UseParallelGC or -XX:+UseParallelOldGC with -XX:+UseConcMarkSweepGC simultaneously, that's why your require -XX:+UseParNewGC to be paired with CMS otherwise use -XX:+UseSerialGC explicitly OR -XX:**-**UseParNewGC if you wish to use serial method against young generation

Solution 3 - Java

UseParNewGC usually knowns as "parallel young generation collector" is same in all ways as the parallel garbage collector (-XX:+UseParallelGC), except that its more sophiscated and effiecient. Also it can be used with a "concurrent low pause collector".

See Java GC FAQ, question 22 for more information.

Note that there are some known bugs with UseParNewGC

Solution 4 - Java

Using -XX:+UseParNewGC along with -XX:+UseConcMarkSweepGC, will cause higher pause time for Minor GCs, when compared to -XX:+UseParallelGC.

This is because, promotion of objects from Young to Old Generation will require running a Best-Fit algorithm (due to old generation fragmentation) to find an address for this object.
Running such an algorithm is not required when using -XX:+UseParallelGC, as +UseParallelGC can be configured only with MarkandCompact Collector, in which case there is no fragmentation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfglezView Question on Stackoverflow
Solution 1 - JavafglezView Answer on Stackoverflow
Solution 2 - JavaIvan HerlambangView Answer on Stackoverflow
Solution 3 - JavaSuraj ChandranView Answer on Stackoverflow
Solution 4 - JavaAvinash GantaView Answer on Stackoverflow