Tuesday, 29 January 2019

G1 Garbage Collector in Java


G1 is introduced in Java7. Oracle 9 Hotspot JVM comes with default G1 Garbage collection. 
One of the good property of this is you can configure this for maximum pause time using flag: 

-XX:MaxGCPauseMillis=n.

Lots of real-world studies say most of the objects (90%) garbage collected in a young generation or in first garbage collection or minor GC (also it depends upon applications). Who survived a couple of GCs(major GC), present in old memory (old objects) they will remain survive more than 95% times.
Explaination on G1 Garbage Collector:
  •  It does most of the work concurrently.
  • It uses non-continuous which enables G1 to deal with the very large heap efficiently.
  • Instead of dividing heaps into 3 spaces (old) like other Garbage Collectors like CMS (concurrent mark and sweep), Parallel etc.
  • it divides heap memory in small chunks. These regions are fix-sized (about 2Mb by default). like below
U: Unassigned, O: Old, S: survivor, E: eden
  • Splitting into small regions helps G1 concurrently run and finish it off very quickly. 
  • While running GC on Eden space all the survived objects get copied to unassigned space. The unassigned space becomes survivor space.
  • If all the objects in Eden space are garbage then it can be declared as Unassigned.
  • G1 is not run on whole heap memory at once like others Garbage Collectors, instead of this it always selects the regions which are full or almost full to minimizes the amount of work to free heap space.
  • G1 only stops the application at the beginning of the GC for bootstrapping , this phase is called as Initial Mark.
  • While Application is executing it follow all the references and mark live objects, this phase called as Concurrent Mark.
  •  When above phase(Concurrent Mark) is done then application again stops. for final cleanup is made, this phase called as Final Mark.
  •  To move objects and reclaim heap memory, this phase called as Evacuation phase this phase is fast, called as Evacuation Phase.
  • This is not good for small heaps then it that case might be full GC is performed and might slow down overall executions. In that case increase the heap size or other Garbage collectors can be used.
Many properties and optimization can be used for G1 Gc. will be covered in an upcoming post.

No comments:

Post a Comment

Generating Unique Id in Distributed Environment in high Scale:

Recently I was working on a project which requires unique id in a distributed environment which we used as a  primary  key to store in dat...