Performance Engineering – GC – Types
Performance Engineering – GC – Types
Types of Garbage Collectors
- Garbage Collection tracks each and every object available in the JVM heap space, and removes the unused ones.
- Basically, GC works in two simple steps, known as Mark and Sweep:
- Mark – this is where the garbage collector identifies which pieces of memory are in use and which aren’t.
- Sweep – this step removes objects identified during the “mark” phase.
- JVM has five types of GC implementations
Serial Garbage Collector
- This is the simplest GC implementation, as it basically works with a single thread. As a result, this GC implementation freezes all application threads when it runs. Therefore, it’s not a good idea to use it in multi-threaded applications, like server environments.
- To enable Serial Garbage Collector, we can use the following argument:
- java -XX:+UseSerialGC -jar Application.java
Parallel Garbage Collector
- It’s the default GC of the JVM, and sometimes called Throughput Collectors. Unlike Serial Garbage Collector, it uses multiple threads for managing heap space, but it also freezes other application threads while performing GC.
- If we use this GC, we can specify maximum garbage collection threads and pause time, throughput, and footprint (heap size).
- To enable parallel Garbage Collector, we can use the following argument:
- java -XX:+UseParallelGC -jar Application.java
- If we use this GC, we can specify maximum garbage collection threads and pause time, throughput and footprint (heap size). The numbers of garbage collector threads can be controlled with the command-line option
- -XX:ParallelGCThreads=<N>
- The maximum pause time goal (gap [in milliseconds] between two GC)is specified with the command-line option
- -XX:MaxGCPauseMillis=<N>
- The maximum throughput target (measured regarding the time spent doing garbage collection versus the time spent outside of garbage collection) is specified by the command-line option
- -XX:GCTimeRatio=<N>
- A maximum heap footprint (the amount of heap memory that a program requires while running) is specified using the option -Xmx<N>.
CMS Garbage Collector
- The Concurrent Mark Sweep (CMS) implementation uses multiple garbage collector threads for garbage collection. It’s designed for applications that prefer shorter garbage collection pauses, and can afford to share processor resources with the garbage collector while the application is running.
- Simply put, applications using this type of GC respond slower on average, but don’t stop responding to perform garbage collection.
- CMS garbage collector holds all the application threads in the following two scenarios only
- During marking the referenced objects in the old-generation space.
- Any change in heap memory in parallel with doing the garbage collection
- In comparison with the parallel garbage collector, CMS collector uses more CPU to ensure better application throughput. If we can allocate more CPU for better performance then CMS garbage collector is the preferred choice over the parallel collector.
- To enable CMS Garbage Collector, we can use the following argument:
- java -XX:+USeParNewGC -jar Application.java
G1 Garbage Collector
- G1 (Garbage First) Garbage Collector is designed for applications running on multi-processor machines with large memory space. It’s available from the JDK7 Update 4 and in later releases.
- G1 collector will replace the CMS collector, since it’s more performance efficient.
- In G1 collector contains two phases;
- Marking
- Sweeping
- Unlike other collectors, G1 collector partitions the heap into a set of equal-sized heap regions, each a contiguous range of virtual memory. When performing garbage collections, G1 shows a concurrent global marking phase to determine the liveliness of objects throughout the heap.
- After the mark phase is completed, G1 knows which regions are mostly empty. It collects in these areas first, which usually yields a significant amount of free space. It is why this method of garbage collection is called Garbage-First.
- To enable G1 Garbage Collector, we can use the following argument:
- java -XX:+UseG1GC -jar Application.java
Epsilon Garbage Collector
- Epsilon is a non-operational or a passive garbage collector. It allocates the memory for the application, but it doesn’t collect the unused objects. When the application exhausts the Java heap, the JVM shuts down. It means Epsilon garbage collector allows, applications to run out of memory and crash.
- We can use this argument to get a heap dump when the application is crashing due to out of memory.
- XX:HeapDumpOnOutOfMemoryError
- If we need to squeeze every bit of performance out of our application, Epsilon might be your best option for a GC. But we need to have a complete understanding of how our code uses memory. If it creates almost no garbage or you know exactly how much memory it uses for the period it runs in, Epsilon is a viable option.
- To enable Epsilon Garbage Collector, we can use the following argument:
- java -XX:+UseEpsilonGC -jar Application.java
Z Garbage Collector
- ZGC (Z Garbage Collector) is a scalable low-latency garbage collector that debuted in Java 11 as an experimental option for Linux. JDK 14 introduced ZGC under the Windows and macOS operating systems. ZGC has obtained the production status from Java 15 onwards.
- ZGC performs all expensive work concurrently, without stopping the execution of application threads for more than 10 ms, which makes it suitable for applications that require low latency. It uses load barriers with colored pointers to perform concurrent operations when the threads are running, and they’re used to keep track of heap usage.
- To enable Z Garbage Collector, we can use the following argument:
- java -XX:+UseZGC -jar Application.java
- jMeter a java application uses the below Garbage Collector
- Below information is for reference, on performance metrics comparison of various Garbage Collectors
- Use the garbage collectors for the below use cases
- G1 G.C – While focusing on the latency
- CMS G.C – While working with common applications
- Parallel G.C – While working with batch applications
- Serial G.C – While working with small devices or when the garbage collection doesn’t affect other applications or CPUs
-By Saravanan & Sai Krishna