Garbage Collection is a process to identify and delete the objects from Heap memory which are not in use. GC frees the space after removing unreferenced objects.
The event in which Garbage Collectors are doing their job is called “Stop the world” event which means all of your application threads are put on hold until the garbage is collected.
How Garbage Collector works
A Java Virtual Machine (JVM) is a software implementation of a physical machine. There are various implementations of JVM. Here, we will be talking about Oracle HotSpot JVM. The basic process of Hotspot JVM Garbage collector completes in two phases:
Phase 1. Marking
This phase is called marking phase in which GC identifies which objects are in use or which are not. All objects are scanned in the marking phase to make this determination.
Phase 2. Deletion
In Deletion phase, the marked object is deleted and the memory is released. Deletion of the unreferenced objects can be done in two ways:
- Normal Deletion: In this phase, all unused objects will be removed and memory allocator has pointers to free space where a new object can be allocated.
- Deletion and Compaction: As you see in normal deletion there are free blocks between referenced objects. To further improve performance, in addition to deleting unreferenced objects, remaining referenced object will be compact.
Why Heap divided into Generations
It is a cumbersome process to scan all of the objects from a whole heap and further mark and compact them. The list of the object grows gradually which leads to longer garbage collection time as more and more objects are allocated with time.
In General Applications most of the objects are short-lived. Fewer and fewer objects remain allocated over time.
That’s why to enhance the performance of the JVM, Heap is broken up into smaller parts called generations and JVM performs GC in these generations when the memory is about to fill up.
Generational Process of Garbage Collection
Now, when you know why heap is divided into generations, it’s time to look into how these generations would interact.
- New objects are allocated in Eden Space of Young Generation. Both Survivor Spaces are empty in starting.
- A minor garbage collection will trigger once the Eden space fills up.
- Referenced objects are moved to the S0 survivor space and Eden Space will be cleared and all unreferenced objects will be deleted.
- It will happen again to Eden space when next time GC will be triggered. However, in this case, all referenced objects are moved to S1 survivor space. In addition, objects from the last minor GC on the S0 survivor space have their age incremented and get moved to S1. Now both Eden and S0 will be cleared, and this process will repeat every time when GC is triggered. On every GC triggered, survivor spaces will be switched and object’s age will be incremented.
- Once the objects reach a certain age threshold, they are promoted from young generation to old generation. So, this pretty much describes how objects promotion takes place.
- The major GC will be triggered once the old generation completely fills up.
Available Garbage collectors in Hotspot JVM
- Serial Garbage Collector: Serial GC designed for the single-threaded environments. It uses just a single thread to collect garbage. It is best suited for simple command-line programs. Though it can be used on multiprocessors for applications with small data sets.
- Parallel Garbage Collector: Unlike Serial GC it uses multiple threads for garbage collection. It is a default collector of JVM and it is also called the Throughput garbage collector.
- CMS Garbage Collector: CMS uses multiple threads at the same time to scan the heap memory and mark in the available for eviction and then sweep the marked instances.
- G1 Garbage Collector: G1 Garbage collector is also called the Garbage First. It is available since Java 7 and its long-term goal is to replace the CMS collector. The G1 collector is a parallel, concurrent, and incrementally compacting low-pause garbage collector.
Points to Remember for tuning the Generations to optimize the performance:
- Based on certain hardware and operating system configurations one of these Garbage Collector is selected by default. The purpose of various garbage collectors are available is to choose appropriate according to the need of your application.
- To explicitly enable the particular GC we can use given (-XX:+UseSerialGC, -XX:+UseSerialGC, -XX:+UseParallelGC, -XX:+UseG1GC) VM options.
- Mainly, two components of JVM are focused on, when tuning performance The Heap and the Garbage Collector.
- Young Generation is the first place to optimize the performance, to define the size of YG we can use -XX:NewSize and -XX:MaxNewSize VM options.
- -Xmx/2 (-Xmx to set the maximum heap size) is an upper bound for -XX:MaxNewSize. This is for stability reason. It is not allowed to choose a young generation’s size larger than the old generation.
- It is also possible to specify the young generation size in relation to the size of the old generation using -XX:NewRation. For example, if we set -XX: NewRatio=3, it means the old generation will be 3 times larger then YG.