Futuregen Skill | Online Courses - Bootcamp & R&D Platform |Online Courses - Learn Anything | No.1 Online Training in Haldwani, Uttrakhand ,India

Best Java Training Insitute with 100% Learning & Implementation

GC Tuning


When memory runs out garbage collector executes two pahese.

MARK: Traces reachable objects

SWEEP:Collects garbage objects

The sweep phase scans the heap looking for objects mark bit with zero.Any such objects are added in the free list .Finally objects with mark bit 1 have their mark bit reset to zero(0) for next garbage collection cycle.Every object has an extra bit called as mark bit reserved for memory management.Initially bit is mark bit as 0Its set to the 1 for reachable object during mark phase.

Safepoint:

GC safepoint is a point or range in thread's execution where collector can identify all references in that thread
execution stack.Bringing a thread to a safe point means " is the act of getting thread to safe point and not executes past it" .There are other types of safepoints that require more information than safepoints(deoptimization) .In a global safepoint , all threads are at the safepoint.it is also called "stop the world pause" .

When Does an Object Become Eligible for Garbage Collection? Describe How the Gc Collects an Eligible Object?

An object becomes eligible for Garbage collection or GC if it is not reachable from any live threads or by anystatic references.The most straightforward case of an object becoming eligible for garbage collection isif all its references are null. Cyclic dependencies without any live external reference are also eligiblefor GC. So if object A references object B and object B references Object A and they don't have any otherlive reference then both Objects A and B will be eligible for Garbage collection.

Precise vs Conservative collection

Collector is conservative if it is unaware about object references at collection time,or is not sure whether a field is refernce or not..A collector is precise if it identify and process all object references at the time of collection.All commercial server jvm use precise collector.
All commercial server jvm use some form of moving collector.

Starting GC Tuning

Start by looking at howyour application behaves, what events fill up the memory space, and what space is filled. Remember that:

Assigned objects in the Eden generation are moved to Survivor space
Assigned objects in the Survivor space are moved to Tenured generation if the counter is high enough or the counter is increased.
Assigned objects in the Tenured generation are ignored and will not be collected.

Serving Different Use Cases

Deciding which garbage collector to use is not always obvious. Its important to understand that to make the correct choice you first need to figure out what your main goals are. Often goals are to optimize for throughput, latency and/or footprint. The optimal solution would of course be to optimize for all of the above and get the best possible performance in every situation. The collectors strive to be as optimal as possible from every aspect, but they are designed to make different tradeoffs to support different use-cases.

A quick summary of what we mean by improving in the different areas:

Throughput: Lower the GCs impact on the total number of transactions that can be completed in a set amount of time
Latency:Lower the GCs impact on any single transaction
Footprint:Lower the additional resources used by the GC

G1 Garbage Collector

G1 (Garbage First) Garbage Collector is designed for applications running on multi-processor machines with large memory space. It's available since JDK7 Update 4 and in later releases.G1 collector will replace the CMS collector since it's more performance efficient.Unlike other collectors, the 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 (i.e. phase 1 known as Marking) to determine the liveness 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 (i.e. phase 2 known as Sweeping). It is why this method of garbage collection is called Garbage-First.

To enable the G1 Garbage Collector, we can use the following argument:

java -XX:+UseG1GC -jar Sample.java

Z Garbage Collector

ZGC (Z Garbage Collector) is a scalable low-latency garbage collector which 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 are used to keep track of heap usage.

To enable the Z Garbage Collector, we can use the following argument in JDK versions lower than 15:

java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC Application.java

From version 15 we don't need experimental mode on:

java -XX:+UseZGC Application.java

Shenandoah Garbage Collector

Shenandoah is a rising star among garbage collectors that can already be considered as the best upcoming solution for JVM vertical scaling.The main difference compared to others is the ability to shrink (uncommit and release unused RAM to OS) asynchronously without necessity to call Full GC. Shenandoah can compact live objects, clean garbage and release RAM back to OS almost immediately after detecting free memory. And the possibility of omitting Full GC leads to eliminating related performance degradation.

Lets see how it works in practice:

java -XX:+UseShenandoahGC -Xmx2g -Xms32m -XX:+UnlockExperimentalVMOptions -XX:ShenandoahUncommitDelay=1000 -XX:Shenan

Conclusion

Java keeps perfecting and adapting to always changing demands. So currently its RAM appetite is no longer a problem for microservices and cloud hosting of traditional applications, as there are already the right tools and ways to scale it properly, clean the garbage and release the resources for the required processes. Being configured smartly, Java can be cost-effective for all ranges of projects from cloud-native startups to the legacy enterprise applications.

Have Queries?

Talk to our Career Counselor for more Guidance on picking the right Career for you! .

ENQUIRE NOW
7.png
shape3.png