G1 garbage collector / Garbage first collector in java


In this core java tutorial we will learn about G1 garbage collector / Garbage first collector in java.



Contents of page >
  • 1. The G1 garbage collector features -
  • 2. Vm (JVM) option for enabling G1 Garbage Collector (or Garbage First) in java >
    • -XX:+UseG1GC
  • 3. G1(Garbage First) collector functioning >
  • 4. When to use G1 garbage collector >
  • 5. When to switch from CMS (or old garbage collectors) to G1 garbage collector >
  • 6. The G1(Garbage First) collector working Step by Step >
    • 6.1. G1(Garbage First) garbage collector Heap Structure >
    • 6.2. G1(Garbage First) garbage collector Heap Allocation >
    • 6.3. Young Generation in G1 garbage collector
      • Young GC in G1 garbage collector
      • End of a Young GC with G1 garbage collector
    • 6.4. Old Generation Collection with G1 garbage collector
      • Initial Mark -
      • Root Region Scanning -
      • Concurrent Marking -
      • Remark (Stop the World Event) -
      • Cleanup (Stop the World Event and Concurrent) -
  • Summary -


G1 Garbage Collector (or Garbage First) in java

1. The G1 garbage collector features -

  • G1 garbage collector is also called
    • G1 garbage collector
    • G1 collector
    • G1 GC (garbage collector)
    • Garbage first collector

  • G1 garbage collector was introduced in Java 7
  • G1 garbage collector was designed to replace CMS collector(Concurrent Mark-Sweep garbage Collector).
  • G1 garbage collector is parallel,
    • G1 garbage collector is concurrent, and
    • G1 garbage collector is incrementally compacting low-pause garbage collector in java.
  • G1 garbage collector has much better layout from the other garbage collectors like serial, throughput and CMS garbage collectors in java.

  • G1(Garbage First) collector is a server-style garbage collector.
  • G1(Garbage First) collector is targeted for multi-processor machines with large memories.
  • G1(Garbage First) collector pause time goals with a high probability, while achieving high throughput.
  • G1(Garbage First) collector is a compacting collector.
  • G1(Garbage First) collector compacts sufficiently to completely avoid the use of fine-grained free lists for allocation, and instead relies on regions.
  • G1(Garbage First) collector allows customizations by allowing users to specify pause times.
  • G1 Garbage Collector (or Garbage First) limits GC pause times and maximizes throughput.



2. Vm (JVM) option for enabling G1 Garbage Collector (or Garbage First) in java >
-XX:+UseG1GC

Example of using G1 Garbage Collector in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseG1GC -jar d:\MyJar.jar

Learn how to pass vmargs (VM parameters) to java program in eclipse?

You must not use -XX:+UseParallelGC with -XX:+UseConcMarkSweepGC.


3. G1(Garbage First) collector functioning >
CMS garbage collectors divides heap into three sections: young generation, old generation, and permanent generation of a fixed memory size.
All memory objects end up in one of these three sections.

The G1 collector takes a different approach than CMS garbage collector in partitioning java heap memory.

The heap is split/partitioned into many fixed sized regions (eden, survivor, old generation regions), but there is not a fixed size for them. This provides greater flexibility in memory usage.

G1 is not a real-time garbage collector.
It meets the set pause time target with high probability but not absolute certainty.
Based on data from previous collections, G1 does an estimate of how many regions can be collected within the user specified target time. Thus, the collector is highly accurate model for garbage collection.

G1(Garbage First) collector is designed for applications that -
    • Can operate concurrently with applications threads.
    • Compact free space quickly.
    • Allows users to specify pause times.
    • doesn’t require a much larger Java heap.


4. When to use G1 garbage collector >
G1 must be used when applications that require large heaps with limited GC latency.
Example - Application that require
  • heaps around 5-6GB or larger and
  • pause time required below 0.5 seconds

5. When to switch from CMS (or old garbage collectors) to G1 garbage collector >
Applications using CMS garbage collector may switch to G1 when >
  • Full GC durations are too long or too frequent.
  • The rate of object allocation or promotion varies significantly.
  • Long garbage collection (longer than 0.5 to 1 second)


6. The G1(Garbage First) collector working Step by Step >

The G1 collector takes a different approach than CMS garbage collector in partitioning java heap memory.


6.1. G1(Garbage First) garbage collector Heap Structure >
The heap is split/partitioned into many fixed sized regions (eden, survivor, old generation regions), but there is not a fixed size for them. This provides greater flexibility in memory usage.

Each region’s size is chosen by JVM at startup.
Generally heap is divided into 2000 regions by JVM varying in size from 1 to 32Mb.


6.2. G1(Garbage First) garbage collector Heap Allocation >
As mentioned above there are following region in heap >
  1. Eden generation region,
  2. survivor generation region,
  3. old generation region

Additionally there is one more region>
  1. Humongous region - These regions hold objects that are 50% the size of a standard region or larger.

Finally one more region >
  1. unused regions of the heap.

Live objects are moved or copied from one region to another.
Regions are designed to be collected in parallel with or without stopping all other application threads.



6.3. Young Generation in G1 garbage collector
Generally heap is divided into 2000 regions by JVM.
Minimum size of region can be 1Mb and
Maximum size of region can be 32Mb.

Regions are not required to be contiguous like CMS garbage collector.


Young GC in G1 garbage collector
  • Live objects are copied or moved to survivor regions.
  • If objects aging threshold is met it get promoted to old generation regions.
  • It is STW (stop the world) event. Eden size and survivor size is calculated for the next young GC.
  • The young GC is done parallely using multiple threads.

End of a Young GC with G1 garbage collector
At this stage Live objects have been evacuated (copied or moved) to >
  • survivor regions or
  • old generation regions.


6.4. Old Generation Collection with G1 garbage collector

G1 collector is low pause collector for old generation objects.

Initial Mark -
  • It is STW (stop the world) event.
  • With G1, it is piggybacked on a normal young GC. Mark survivor regions (root regions) which may have references to objects in old generation.

Root Region Scanning -
  • Scan survivor regions for references into the old generation.
  • This happens while the application continues to run. The phase must be completed before a young GC can occur.

Concurrent Marking -
  • Find live objects over the entire heap.
  • This happens while the application is running.
  • This phase can be interrupted by young generation garbage collections.

Remark (Stop the World Event) -
  • Completes the marking of live object in the heap.
  • Uses an algorithm called snapshot-at-the-beginning (SATB) which is much faster than algorithm used in the CMS collector.

Cleanup (Stop the World Event and Concurrent) -
  • Performs accounting on live objects and completely free regions. (Stop the world)
  • Young generation and old generation are reclaimed at the same time
  • Old generation regions are selected based on their liveness.

  • Scrubs the Remembered Sets. (Stop the world)
  • Reset the empty regions and return them to the free list. (Concurrent)


Differences >
G1(Garbage First) collector offers more predictable garbage collection pauses than CMS collector which allows users to specify desired pause times. xxd



Summary -

I am highlighting all the summary portion to keep it separate from whole tutorial. Please refer above for complete detail.

1. The G1 garbage collector features -
  • G1 garbage collector was introduced in Java 7
  • G1 garbage collector was designed to replace CMS garbage Collector.
  • G1 garbage collector is parallel and concurrent, and
  • G1 Garbage Collector (or Garbage First) limits GC pause times and maximizes throughput.


2. Vm (JVM) option for enabling G1 Garbage Collector (or Garbage First) in java >
-XX:+UseG1GC


3. G1(Garbage First) collector functioning >

CMS garbage collectors divides heap into three sections: young generation, old generation, and permanent generation of a fixed memory size.

The heap is split/partitioned into many fixed sized regions (eden, survivor, old generation regions), but there is not a fixed size for them. This provides greater flexibility in memory usage.


4. When to use G1 garbage collector >
G1 must be used when applications that require large heaps with limited GC latency.

5. When to switch from CMS (or old garbage collectors) to G1 garbage collector >
Applications using CMS garbage collector may switch to G1 when >
  • Full GC durations are too long or too frequent.

6. The G1(Garbage First) garbage collector working Step by Step >
The G1 collector takes a different approach than CMS garbage collector in partitioning java heap memory.


6.1. G1(Garbage First) garbage collector Heap Structure >
The heap is split/partitioned into many fixed sized regions (eden, survivor, old generation regions).

6.2. G1(Garbage First) garbage collector Heap Allocation >
Live objects are moved or copied from one region to another.

As mentioned above there are following region in heap >
Eden, survivor and old generation region.
Also, Humongous and unused regions are there in heap.


6.3. Young Generation in G1 garbage collector
Generally heap is divided into 2000 regions by JVM.
Minimum size of region can be 1Mb and
Maximum size of region can be 32Mb.


Young GC in G1 garbage collector
  • Live objects are copied or moved to survivor regions.
  • If objects aging threshold is met it get promoted to old generation regions.
  • It is STW (stop the world) event. Eden size and survivor size is calculated for the next young GC.

End of a Young GC with G1 garbage collector
At this stage Live objects have been evacuated (copied or moved) to >
survivor regions or old generation regions.

6.4. Old Generation Collection with G1 garbage collector

Initial Mark -
  • It is STW (stop the world) event.
  • Mark survivor regions (root regions) which may have references to objects in old generation.

Root Region Scanning -
  • Scan survivor regions for references into the old generation.
  • This happens while the application continues to run.

Concurrent Marking -
  • Find live objects over the entire heap.
  • This happens while the application is running.

Remark (Stop the World Event) -
  • Completes the marking of live object in the heap.

Cleanup (Stop the World Event and Concurrent) -
  • Performs accounting on live objects and completely free regions. (Stop the world)
  • Young generation and old generation are reclaimed at the same time

  • Reset the empty regions and return them to the free list. (Concurrent)

Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

 

Different type of garbage collectors in java>

>Serial collector / Serial GC (Garbage collector) in java

>Throughput GC (Garbage collector) or Parallel collector in java

>Concurrent Mark Sweep (CMS) collector / concurrent low pause garbage collector in java

>PS Scavenge and PS MarkSweep




Monitor, analyze garbage collection and fix MEMORY LEAK >

>How to monitor and analyze the garbage collection in 10 ways in java

>Detecting and fixing memory leak in java


eEdit
Must read for you :