Меню

Java Garbage Collector

Java, Garbage Collector is an important aspect that differentiates it from languages that do have not automatic GC, Let's dive a bit into GC, and see how many types of GC we have and what kind of spaces we have in heap in Java. Before diving into the part, there are steps that the Garbage collector does when called by JVM. Steps are mark, delete/sweep, and compacting. Mark ->start from main, goes through all object life cycle, and mark live objects(referenced objects) Delete/Sweep - Delete dead objects(unreferenced objects) Compacting - Trying to make memory compact(contiguous) after the delete/sweep stage. The above steps can be done by various GC types with various approaches.

Serial Collector - runs in the single-threaded mode, which stops the application. Concurrent Collector - In this step, GC runs with an application that makes apps work without stopping except for the stage mark/remark. Parallel Collector - Use multiple Cpu to perform GC. The difference from Serial Collector is that serial does stages(mark, sweep, compact) in single-threaded mode, but it does in parallel. There is also G1 GC which came in the 1.7 version. It splits heap into boxes and runs on boxes concurrently which have more unreferenced objects that bring lower stop time.

garbage collector

We can mainly divide JVM heap space 2 generations. Young and old generation. First, we can observe the young generation. We can divide the Young generation into 2 parts: Eden space and Survivor Space. Also, Survivor space consists of 2 parts: S0 and S1. Eden space is the first place that an object is allocated when we create. For example in java when we execute a new Object() statement it allocated space in the Eden space. When Eden space is filled up Minor garbage collection event happened. In the first work of GC, it will mark referenced/unreferenced objects in Eden space then it will deallocate memory of unreferenced object and move the referenced object to S0 space and increment the age of an object to 1. In the next minor garbage collection event, the GC again marks referenced/unreferenced objects in both Eden space and S0 space then it will deallocate the memory of unreferenced object in both Eden and S0 space and move a referenced object from S0 and Eden space to S1 space and also increment the age of this object.

In the next minor garbage collection event, the GC again marks referenced/unreferenced objects in both Eden space and S1 space then it will deallocate memory of unreferenced object in both Eden and S1 space and move a referenced object from S1 and Eden space to S0 space and also increment the age of this object. This process is repeated. There is some threshold that when object age is reached that number, For example, 8 is our threshold and when our object age reaches 8, GC move this object from Young generation to Old generation. This is called Promotion.

Collecting garbage from the Old generation memory space is called Major garbage collection. When the old generation is full, a major garbage collector is called. After cleaning heap space, compacting process happens in memory.