Comments:"Brief comparison of Java 7 HotSpot Garbage Collectors | omsn.de Blog"
URL:http://www.omsn.de/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, calledErgonomics 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
- In short: does GC concurrently to application thread
- JVM flag:
-XX:+UseConcMarkSweepGC
- Execution Mode: Concurrent
- Young generation cleanup: Parallel
- Old generation cleanup: Single-threaded during concurrent collection, Parallel during pause times
- Best for: don't use this, use G1 instead (see next)
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: