Java Garbage Collection

Ashan Tharuka Indrajith
6 min readApr 20, 2020

--

What is garbage in java

An unused object or an unreferenced object which is no longer a part of your program is garbage.

How an object can be unreferenced

there are three ways object can be unreferenced,

1. when the object goes out of scope permanently

2. when an object’s reference variable is assigned an explicit null value

3. when an object’s reference variable is assigned another object

What is java Garbage Collection?

Garbage collection is a process of looking at the heap memory, identifying which objects in use and which are not and deleting unused objects. It makes java memory efficient.

Benefits of garbage collection

The most significant benefit of Java garbage collection is that it takes care of memory heap automatically by deleting unused objects when memory gets low. Programming languages like c and c++ haven’t automatic garbage collection. Therefore, programmers have to implement manual memory management in their code.

Basic operations of garbage collection

1) Marking Reachable Objects

Garbage Collector Roots

GC Roots are local variable and input parameters of the currently executing methods, static field of the loaded classes active threads that hold an object in the heap memory. Garbage Collection needs the roots to determine if the object is live or not. If an object is reachable by any of the roots, then it is not garbage. If not, it is garbage and needs to be deleted.

According to the above picture, every object the GC visits is marked as alive. Live objects are represented as blue. When the marking phase finishes, every live object is marked. All other objects (grey data structures on the picture above) are unreachable from the GC roots, implying that your application cannot use the unreachable objects anymore. This is how GC identify unused objects.

2) Removing Unused objects

Marking Sweeping & Compacting

Mark and Copy

Generational garbage collection

Memory pools holding objects of different ages. Garbage collection occurs in each generation when the generation fills up.

Young Generation: Newly created objects start in the Young Generation. The Young Generation is further subdivided into an Eden space, where all new objects start, and two Survivor spaces, where objects are moved from Eden after surviving one garbage collection cycle. When objects are garbage collected from the Young Generation, it is a minor garbage collection event.

Old Generation: Objects that are long-lived are eventually moved from the Young Generation to the Old Generation. When objects are garbage collected from the Old Generation, it is a major garbage collection event.

Permanent Generation: Metadata such as classes and methods are stored in the Permanent Generation. Classes that are no longer in use may be garbage collected from the Permanent Generation.

During a full garbage collection event, unused objects in all generations are garbage collected.

Different types of Garbage Collectors

1. G1 Garbage Collector

Firstly G1 Garbage Collector is introduced in JDK 7. G1 Garbage Collector is the default garbage collection of Java 9. G1 Garbage Collector is used for large heap memory areas.

Implementation: If we are using Java version less than 9 and we want to use G1 Garbage Collector then we have to mention explicitly while running jar file like:

java -XX:+UseG1GC -jar application.java

2. Serial Garbage Collector

This GC implementation freezes all application threads when it runs.
It uses just a single thread for garbage collection. it is not a good idea to use Serial GC in multi-threaded applications like server environments.

java -XX:+UseSerialGC -jar application.java

3. Parallel Garbage Collector

Parallel Garbage Collector is the default garbage collector in Java 8. It is also known as Throughput collector. Parallel Garbage Collector is same as Serial Garbage Collector because Parallel Garbage Collector also freezes the running threads of the application while performing the garbage collection. But the difference is, Parallel Garbage Collector uses multiple threads to perform cleaning of unused heap area.

java -XX:+UseSerialGC -jar application.java

4. Epsilon Garbage Collector

Introduced in Java 11. It handles memory allocation but doesn’t recycle it when objects are no longer used. When your application exhausts the Java heap, the JVM shuts down. In other words, Epsilon will help you to see how garbage collection affects your app’s performance, and it allows you to run your application until memory runs out.

java XX:+UseEpsilonGC -jar application.java

5. Z Garbage Collector

ZGC is a low-latency GC designed to work well with huge amounts of memory. The Oracle documentation refers to multi-terabyte heaps in its description of Z. It manages these heaps with pause times under 10ms and little impact on throughput. These times are better than G1’s. Oracle introduced ZGC in Java 11. In Java 12, Oracle added performance fixes and class unloading even though Z is still in experimental status. It’s only available on 64-bit Linux.

java -XX:+UseZGC -jar application.java

6. Shenandoah Garbage Collector

Shenandoah is another garbage collector with low pause times. Shenandoah was developed at Red Hat and has been around for several years. It’s now part of the Java 12 release. Offers the same advantages as Z GC with large heaps but more tuning options.

java -XX:+UseShenanodoahC -jar application.java

Enable garbage collection logging

By enabling garbage collection logs, it provides information regarding your JVM memory utilisation, the garbage collector work and the overall performance of your application.

There are different ways in terms of how you activate garbage collection logging for Java 8 and earlier and for the newer Java versions.

For Java 8 and earlier, you should add the following flags to your JVM based application startup parameters:

-XX:+PrintGCDetails -Xloggc:<PATH_TO_GC_LOG_FILE>

Where the PATH_TO_GC_LOG_FILE is the location of the garbage collector log file.

For example:

java -XX:+PrintGCDetails -Xloggc:/myapp/gc.log -jar application.jar

For Java 9 and newer you can simplify the command above and add the following flag to the application startup parameters:

-Xlog:gc*:file=<PATH_TO_GC_LOG_FILE>

For example:

java -Xlog:gc*:file=/myapp/gc.log -jar application.jar

Understanding GC logs is not easy. So, there are several tools that we can use to analyse garbage collection logs easily.

For example : GCViewer, GCeasy

References

- https://plumbr.io/handbook/garbage-collection-algorithms

- https://sematext.com/blog/java-garbage-collection-logs

- https://dzone.com/articles/java-garbage-collection-3

- https://stackify.com/what-is-java-garbage-collection

- https://www.geeksforgeeks.org/types-of-jvm-garbage-collectors-in-java- with-implementation-details/

--

--