Epsilon GC and ZGC : New Garbage Collectors in JDK 11

Garbage Collectors in JAVA

  1. Serial GC : The serial GC runs on a single thread. Whenever the generation in heap gets full, the application will stop/pause and GC will be performed clearing the unreachable objects.
  2. Parallel GC : The parallel GC uses multiple CPU’s/threads to perform garbage collection i.e multiple threads are responsible for peforming all the three steps mentioned above.
  3. Concurrent GC : The concurrent GC involves a single thread which will run concurrently with our java application. It doesn’t pause the main application while deleting or compacting but pause the main application for a short period of time during the initial mark and remark phases.

WORKING OF G1 GC

Types of Garbage Collectors.

Epsilon : A No-Op Experimental Garbage Collector

  1. Epsilon GC can be mainly used for performance testing : like how garbage collection affects your application performance and what is your application memory threshold.If you think that your application will need 4GB of memory then we can run it with argument -Xmx4gand if JVM crashed, we can rerun it with -XX:HeapDumpOnOutOfMemoryError enabled and then can take a look at the heap dump to know where or which process has consumed more memory.
  2. Also Epsilon GC is useful for applications where we have to do a last-drop latency improvements i.e for ultra latency sensitive applications, where we are consious about memory allocations and know the application memory footprint and where accepting the GC cycle can be an issue. In those applications, long GC cycle can be a problem because that prolongs the detection of the failure, and ultimately delays recovery. In that cases letting load balancers figure out failover — is sometimes a better recovery strategy than accepting a GC cycle.

Hands On

class GarbageMemoryEpsilon{    static final int MEGABYTE_IN_BYTES = 1024 * 1024;
static final int ITERATION_COUNT = 1024 * 10;
static void main(String[] args) {
System.out.println("Starting pollution");
for (int i = 0; i < ITERATION_COUNT; i++) {
byte[] array = new byte[MEGABYTE_IN_BYTES];
}
System.out.println("Terminating");
}
}
Starting pollution
Terminating
-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC
Starting pollution
Terminating due to java.lang.OutOfMemoryError: Java heap space

ZGC: A Scalable and Experimental Low-Latency Garbage Collector

Colored Pointers — JDK 11 & 12 features.
  • finalizable bit — the object is only reachable through a finalizer​.
  • remap bit — the reference is up to date and points to the current location of the object.​
  • marked0 and marked1 bits — these are used to mark reachable object

Working of ZGC

  • ZGC divides memory into regions, also called​ ZPages. Z-Pages can be dynamically created and ​destroyed.
  • ZGC works concurrently with your application, performing all its work in its threads. It uses load barriers for heap references. Load barriers cause fewer delays than those imposed by the G1 collector’s pre- and post-write barriers.
  • ZGC takes advantage of 64-bit pointers with a technique called pointer coloring. Colored pointers store extra information about objects on the heap. (This is one of the reasons it’s limited to the 64-bit JVM.) By limiting the GC to 4TB heaps, the developers have 22 extra bits in each pointer to encode additional information. Z uses four extra bits at the moment. Each pointer has a bit for finalizable, remapped, mark0, or mark1.
  • The Z garbage collector remaps objects when the memory becomes fragmented. The mapping avoids the performance hit incurred when the GC needs to find space for a new allocation. Pointer coloring helps with remapping since a remapped reference discovers the new location at the next access.
  • When your application loads a reference from the heap, ZGC checks the extra bits. If it needs to do any extra work (e.g. getting a remapped instance), it handles it in the load barrier. It only has to do this once, when it loads the reference. This sets it apart from the write barriers used by mainline garbage collectors like G1.
  • The Z garbage collector performs its cycles in its threads. It pauses the application for an average of 1 ms. The G1 and Parallel collectors average roughly 200 ms.
  • We can enable ZGC with the following command-line options when running our application:
-XX:+UnlockExperimentalVMOptions -XX:+UseZGC

CONCLUSION

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nitin Bisht

Nitin Bisht

3 Followers

Software Engineer, Tech Blogger, otaku. I love to code and write for developers.