Garbage Collection with Java

Table of contents

No heading

No headings in the article.

Garbage collection mechanism is must and well known in each computer language but implementation may be different in each language with variety of technics use to achieve better performance of application and memory utilization.

In java Garbage Collector plays important roll as a Java don't allow developer to handle garbage collection by its own. JVM is responsible for collections at it is use Garbage Collector (GC) for that.

Garbage Collector (GC) normally do is :

  • Find the no longer reachable objects
  • Deallocate the memory
  • Compacting the heap

Garbage Collector is nothing but the program which use one or more threads to perform above listed operations.

Java introduced number of Garbage Collectors with different JDK versions

Each Garbage collector have there own way to work for better performance and memory utilization. we can not say that anyone of them is better or worst , its depend on requirement and size of the applications.

Collectors also categories in Basic and Advance type.

Basic collects ways of working is,

  • Stop all application threads
  • Mark unreachable objects
  • free the memory and compact the heap
  • Resume all application threads

Advance collectors way of working is,

  • Scan all the unreachable objects while all application threads are running.
  • Only pause the application thread when free the memory and compact heap operation.
  • This type of collectors known as concurrent collectors

Serial Collector - This is basic GC, which is use only one thread to process on the heap for garbage collection.

  • This GC use great to use when you have requirement for multiple small JVMs in one machine and small live data set.
  • This garbage collector fully stop all threads.

Parallel GC - In multicore CPU environment parallel GC is used by default.

  • This garbage collector is faster because it is use multiple threads to process on heap.
  • This garbage collector fully stop all threads.

CMS Collector - This GC trace all reachable objects of the heap and mark all unreachable objects of the heap when all threads of application is in running state.

  • In first phase of the this operation all unreachable objects get marked by JVM and in second phase they collected and memory got freed.
  • after both phases live objects from the heap compacted and freed memory get ready for use.
  • CMS Collector is deprecated in Java 9 and replaced with G1GC

G1GC Collector - This is default collector in Java 9 and above

  • This GC is designed for multicore CPU environment and large heap size environment
  • This GC tries to achieve best throughput and latency both

Reference counting GC - Some garbage collectors way of working is they always count the reference of the object and if any counters go with value 0 then that is consider as unreachable or garbage object and that is get collected in next operation.

GC Tuning Tips

  • when heap size is too small for application execution then garbage collection demand is increase more than limit so in that case JVM use multiple GC instance to free memory and it have impact on application throughput.
  • when heap size is too large for application execution then garbage collector pause the application threads too long for travel and collect unreachable objects from the heap. this process become lengthy and that's why response time would be affected.