Garbage Collectors in the J2SE 5.0 HotSpot JVM

Below is the summary on different garbage collectors prepared while reading Memory Management in the Java HotSpot™ Virtual Machine whitepaper. Hope you find it useful!

Garbage Collectors Summary: Serial, Parallel, Parallel Compacting and Concurrent Mark-Sweep (CMS)

Notable points in Concurrent Mark-Sweep Collector:

  • Also known as low-latency collector as it minimizes pause times
  • Some phases are executed concurrently with the application
  • Only garbage collector that does not perform compaction of old generation after major collection
  • Because no compaction is performed, extra heap sizes may be required
  • Has an option of running in Incremental Mode (–XX:+CMSIncrementalMode): in this mode concurrent phases are done incrementally (useful when running on machines with fewer processors to yield back processing to the application)

Related links:

Full Garbage Collections Manifested as Spikes in Respones Times

While performance testing a web application deployed on Tomcat, we noticed that the average response time was about 0.5 seconds while some requests were getting response times of 2.0 seconds. In JConsole we noticed see-saw pattern in memory usage — more importantly we noticed that there were 20 major garbage collections and garbage collector used in PS MarkSweep.

JConsole - Full Garbage Collection

Average time for a major garbage collection is 1.4 seconds. Since PS MarkSweep garbage collector pauses the application when performing a major garbage collection, the response times for requests arriving when major garbage collection is performed is request time + full GC time – i.e., 0.5 + 1.4 ~ 2.0 seconds

After increasing the heap size (thereby giving more memory for young generation and the two survivor spaces) no full major garbage collections and spikes in response times were noticed.JConsole - No Full Garbage Collection

Rationale: When a minor garbage collection is performed, active objects in Eden and survivor space (holding objects) are copied into the empty survivor space. Any objects that couldn’t be copied into survivor space (because of it getting full) are copied into tenured region. After a few such minor collections, tenured region gets full and a full garbage collection is performed to free up space in tenured region. By increasing the heap space, survivor space is increased and the new size is large enough to hold all active objects when a minor garbage collection is performed and a copy to tenured region is avoided.