Friday, February 2, 2018

Garbage Collector

Definition: Garbage collector (GC) is the program running in the background that looks into all the objects in the memory and find out objects that are not referenced by any part of the program. All these un-referenced objects are deleted and space is reclaimed for allocation to other objects.
Working of garbage collector process:
1. Concurrent Marking and Sweep Algorithm:  (Mostly used)
This is the first step where garbage collector identifies which objects are in use and which are not in use
Normal Deletion: Garbage Collector removes the unused objects and reclaim the free space to be allocated to other objects
Deleting with Compacting: For better performance, after deleting unused objects, all the survived objects can be moved to be together.
This will increase the performance of allocation of memory to newer objects.
2. Serial Garbage collector:
Only one garage collector thread works all application threads stopped.
3. Parallel Garbage collector:
Multiple threads for garbage collector works in parallel but all other application threads stops.
Memory Model in Garbage Collector (Internal structure):


There are two generations in Garbage collector Younger Generation, Old Generation

  • Younger generation is the place where all the new objects are created
  • The garbage collection in younger generation is called Minor GC
  • Most of newly created objects are located in the Eden Memory space.
  • When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to one place of the survivor spaces.
  • Minor GC also checks the survivor objects and move them to other survivor space. So at a time one of the survivor space is always empty.
  • Objects that are survived after many cycles of GC are moved to the Old generation memory space.
  • Usually it is done by setting a threshold for the age of the young generation objects before they become eligible to promote to old generation.
  • Old Generation memory contains the objects that are long lived and survived after many rounds of Minor GC. Usually garbage collection is performed in Old generation memory when it is full.
  • Old Generation Garbage collection is called Major GC
  • By default full major garbage collector cycle will run for every 30 mins.
  • Meta Data is not a part of Heap Memory.
  • Meta Data is populated by JVM at runtime based on the classes used by the application.
  • Meta Data also contains Java SE (Java Standard Edition) library classes and methods.
  • Meta Data Objects are garbage collected in a full garbage collection.

Younger generation is divided into three parts
1.    a) Eden Memory
2.    b) Other two parts Survivor Memory (S0, S1)
Eden Memory and Survivor Memory:
Meta Data: (From Java8 version on-words PermGen is removed and metaspace (or) Meta data is placed)
It contains the application metadata required by the JVM (Java Virtual Machine) to describe the classes and methods used in the application.
Garbage collector logs can be analysed using GC viewer tool and Easy GC tool.

No comments :

Post a Comment