Thursday, May 28, 2015

Java Garbage Collection - Concepts -Part 3 - GC Types

Serial Collector:
Both young and old collections are done serially, in a stop-the-world fashion.

Young Generation:

  • Eden to one Survivor
  • From Survivor To Survivor
  • From Survivor Old generation
  • Survivor spaces swap roles (From becomes To and vice-versa)

Old Generation:

  • Old and Permanent generations are collected via a mark-sweep-compactcollection algorithm
  • Mark: Mark all Live objects
  • Sweep: Sweep them to another location, what’s left is garbage
  • Compact: Compact the freed up memory
  • After this, moving objects from Young generation to Old uses bump-the-pointer technique.

How to enable: -XX:+UseSerialGC

Default choice in Java 5
As of Java 6, to be used only in client applications –not recommended for server class machines or enterprise applications.

Parallel Collector :
Also known as Throughput Collector. Takes advantage of multiple CPUs

Young Generation: Parallel version of the Serial Collector Algorithm, still stop-theworld.

  • Eden to one Survivor
  • From Survivor To Survivor
  • From Survivor Old generation
  • Survivor spaces swap roles (From becomes To and vice-versa)

Old Generation: Same as Serial Collector

  • Old and Permanent generations are collected via a mark-sweep-compact collection algorithm
  • Mark:Mark all Live objects
  • Sweep: Sweep them to another location, what’s left is garbage
  • Compact: Compact the freed up memory
  • After this, moving objects from Young generation to Old uses bump-the-pointer technique.

How to enable: -XX:+UseParallelGC

Recommended for:

  • Machines with more CPUs
  • Applications that do not have pause time constraints (long pauses are OK); batch jobs

Parallel Compacting Collector:
Young Generation: Same as parallel collector
Old/Perm Generation: In Parallel, reduces pause times
  • Division into multiple regions followed by three phases
  • Mark Phase: Mark live objects in each region (Parallel)
  • Summary Phase: Identify where maximum space can be reclaimed by compaction
  • Compact Phase: Copy objects into region locations identified by summary phase.
How to enable: -XX:+UseParallelOldGC
Available from Java 5 update 6

Recommended for:
  • Machines with more CPUs
  • Not suitable for applications running on large shared machines, where no single application should monopolize several CPUs for extended periods of time.
  • On such machines, either decrease number of threads used for GC (-XX:ParallelGCThreads=n) or select a different collector.

Concurrent Mark-Sweep (CMS) Collector
Faster response Times
Observation: Young generation collections do not typically cause long pauses. Old generation collections, though infrequent, can impose long pauses, especially when large heaps are involved

Young Generation: Same as Parallel Collector

Old/Perm Generation: In Parallel, reduces pause times
  • Initial mark: Identify live objects reachable (STW)
  • Concurrent Mark: Mark all objects transitively reachable
  • Remark: Identify any modified objects during concurrent mark (STW)
  • Concurrent Sweep: Reclaim Garbage
Disadvantages: Larger Heap Sizes needed, Floating Garbage
How to enable: -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=n, -XX:+CMSIncrementalMode
Available from Java 5 update 6

Recommended for:
  • Application that needs shorter garbage collection pauses
  • Applications that have a relatively large set of long-lived data and that run on machines with two or more processors: web containers.
Java 8 and Improved G1 Collector

  • The Garbage first collector (G1) introduced in JDK 7 update 4 was designed to better support heaps larger than 4GB.
  • The G1 collector utilizes multiple background threads to scan through the heap that it divides into regions, spanning from 1MB to 32MB (depending on the size of your heap).
  • G1 collector is geared towards scanning those regions that contain the most garbage objects first, giving it its name (Garbage first).
  • There is a chance of the heap being depleted before background threads have finished scanning for unused objects, in which case the collector will have to stop the application which will result in a STW collection.
  • G1 also has another advantage that is that it compacts the heap on-the-go, something the CMS collector only does during full STW collections.
  • String deduplication: Since strings (and their internal char[] arrays) takes much of the heap, a new optimization has been made that enables the G1 collector to identify strings which are duplicated more than once across the heap and correct them to point into the same internal char[] array.
  • Metaspace: Classes & class loaders in permgen moved to MetaSpace. This is native memory and could grow up to the underlying infrastructure provided maximum. Classes and class loaders are no longer bound by heap space or permgen sizes.