Sunday 3 March 2019

Garbage Collector

A Garbage Collector is a Java program which tracked the referenced (live) objects and allowed them to keep in the heap memory whereas the memory of the unreferenced (dead) objects is reclaimed and reused for future object allocation. Here, one point to be noted that there is no explicit deletion and no memory is given back to the operating system. As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced (dead) and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory. This process is also called as automatic memory management.

Type of Garbage Collector: 
There are four types of Garbage Collectors:
1. Serial Garbage Collector
2. Parallel Garbage Collector
3. CMS Garbage Collector
4. G1 Garbage Collector
Garbage Collection method involves:
1. Mark: To mark the generation of the object. 
2. Delete/Sweep: To delete the unreachable/dead object.
3. Compact: To compact the memory by moving around the object.
Some important terms:
1. Live Object: The object which is referenced by another object
2. Dead Object: The object which is not referenced by any other object and unreachable
3. Daemon Thread: Garbage collection process is carried out by Daemon Thread
4. System.gc(): It is used to invoke the garbage collector and on invocation, the garbage collector will run to reclaim the unused memory space. The System.gc() is a static method.

Java objects are created in Heap and this heap memory is divided into smaller parts or generations so that the performance of the object allocation can be increased. This object allocation behaviour is used to enhance the performance of the JVM as well. Therefore, the heap is broken up into 3 major parts or generations. 

These heap generations are: 
1. New or Young Generation: New Generation is further divided into three parts known as Eden space, Survivor 1 and Survivor 2 space. When an object first created in heap, memory gets allocated under new generation inside Eden space and after subsequent minor garbage collection if an object survives it gets moved to survivor 1 and then survivor 2. Minor garbage collections can be optimized assuming a high object mortality rate.

2. Old or Tenured Generation: The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually, the old generation needs to be collected. This event is called a major garbage collection. Major garbage collection reclaimed the memory of the old generation.

3. Permanent Generation: This space is reserved and used by JVM to store Metadata about classes and methods, String pool and Class level details. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here.

How does GC work?

Object Creation and Memory Allocation: When new objects are created by JVM then heap memory is allocated to each object. To understand the GC in brief I took one example. 
Let's consider the limit of Eden is 5 i.e. only 5 objects can accommodate in Eden at a time. I named these objects as A, B, C, D and E. When these 5 objects are created, respective memory is allocated to them in Eden under young generation.  



When Eden limit is over: After some time, some of the objects become alive and some of the objects become dead. You can call the live object as referenced objects because those objects are referenced by some other objects and will be used in near future. The objects who become dead are called as unreferenced objects. 
In our example, you can see object A, B and E are live objects whereas object C and D are dead objects. 



Minor Garbage Collection 1: The minor garbage collection is an intermediate process which runs when Eden memory limit is over. The simple definition of Minor GC is to move the live objects from Eden to Survivor 1 (or Survivor 2) when Eden memory limit is over. There is no impact on old (Tenure) generation due to the execution of minor GC. During minor GC, 2 tasks happen:
a. Moving the live objects from Eden to S1 or S2
b. Remove the dead objects and reclaim the memory for new objects
In the figure you can see that object A, B and E have been moved to S1 and memory is reclaimed by removing the dead object C and D.



Again New Object Creation and Memory Allocation along with unreferenced objects: One more time the first step repeats and new objects are created by JVM and heap memory is allocated to each object. This time new objects F, G, H, I and J are created and Eden memory space is allocated to them. The only difference in the first step and this step is; this time some objects are available in the S1 as well, so now total we have 8 objects. During this activity, some of the objects in the Eden as well as in S1 may dead like object B and I. 


Minor Garbage Collection 2: Since the Eden memory is full so this situation again leads minor GC. Here one point to note that minor GC is a periodic process whenever Eden memory limit is over and every time it does the same tasks which I have mentioned above. Now, the live objects F, G, H and J will move to S2 along with A and E which exist in S1. The dead objects B and I will be removed and Eden memory will be reclaimed. 



Promotion: There is one more process which takes place along with all above-mentioned activities i.e. Object Promotion. After each minor GC, the objects become old and their age keeps on increasing. When aged objects reach a certain age threshold they are promoted from young generation to old generation. This process is called as Promotion. 
In our example you can see object A, G, J and O are promoted to old generation after 'n' number of minor GC executions. The promotion process leads to fill old generation memory space. One important point to note down here some of the objects like String and Array objects are directly created in tenured generation memory space.    

Major GC: Major GC cleans up the old generation. The task of Major GC is as same as the minor GC, but the only difference is minor GC reclaims the memory of the young generation whereas major GC reclaims the memory of the old generation. It is also said that many major GCs are triggered by minor GCs.

Full GC: A Full GC is triggered whenever the heap fills up. In such a case the young generation is swept first followed by the old generation. If the old generation is too full to accept the objects of the young generation, the young generation GC is omitted and the old generation GC (major GC) is used to clean and compact the full heap, either in parallel or serial. Either way, the whole heap is collected with a stop-the-world event.  

Related Term:
Stop the World Event: It is a time when all the application threads are stopped until the garbage collection operation completes. Both minor and major garbage collections are "Stop the World" events. A major GC is much slower than minor GC because it involves all live objects.


Reference:- https://perfmatrix.blogspot.com/