omsn.blog

Brief comparison of Java 7 HotSpot Garbage Collectors

First thing to know about HotSpot Garbage Collectors is that in most cases you don't have to know about them. HotSpot has a mechanism built in, called Ergonomics which - based on your operating system, number of CPUs, RAM, etc. - automatically chooses the GC which is most likely the best for your system.

However, if you find yourself in the situation where you believe changing the default collector might improve your application's performance, I summarized the most important differences between the various GCs for you:

Serial GC

  • In short: The simplest GC
  • JVM flag: -XX:+UseSerialGC
  • Execution mode: Stop-the-world
  • Young generation cleanup: Single-threaded
  • Old generation cleanup: Single-threaded
  • Best for: JVMs running on single-core machines. Even on multi-core machines this collector shows the best performance if the data set is smaller than 100 MB.

Parallel GC

  • In short: GC which collects the young generation multi-threaded
  • JVM flag: Before JDK 6: -XX:+UseParallelGC. Now replaced by Parallel Compact GC (see next)
  • Execution Mode: Stop-the-world
  • Young generation cleanup: Parallel
  • Old generation cleanup: Single-threaded
  • Best for: See Parallel Compact GC

Parallel Compact GC

  • In short: GC which collects both young and old generation multi-threaded
  • JVM flag: Before JDK 6: -XX:+UseParallelOldGC. Since JDK 6: -XX:+UseParallelGC or -XX:+UseParallelOldGC.
  • Execution Mode: Stop-the-world
  • Young generation cleanup: Parallel
  • Old generation cleanup: Parallel
  • Best for: Applications where the overall execution time performance is of higher importance than low pause times.

Concurrent Mark Sweep

Garbage First

  • In short: Newest and most sophisticated GC so far. Uses heuristics to predict pause times and heap regions most likely to be filled up. Intended to replace Concurrent Mark Sweep Collector.
  • JVM flag: -XX:+UseG1GC
  • Execution Mode: Concurrent
  • Young generation cleanup: Parallel
  • Old generation cleanup: Parallel
  • Best for: Applications where low pause times are of higher importance than overall execution time, like web servers.

Sources:


comments powered by Disqus