JVM Garbage Collection

Garbage Collection (GC) is a mechanism provided by JVM to reclaim heap space from objects which are eligible for GC. Garbage Collection is performed by a daemon thread called Garbage Collector. Before performing GC on the heap, JVM performs finalize() method to ensure cleanups of the objects. GC are performed automatically triggered by JVM based on Java Heap Size.

If there’s no memory space for creating new object in Heap, JVM throws OutOfMemoryError

An Object becomes eligible for GC if the object is not reachable for any live threads or any static reference (Cyclic Dependencies are not counted as reference)

Heap can be divided into three parts a. Young Generation [Eden Space / Survivor 1 / Survivor 2] b. Tenured / Old Generation c. Perm Area of Heap

Types of Garbage Collector in Java

Throughput Garbage Collector uses parallel version of the young generation collector

Concurrent Low Pause Collector uses Tenured Generation and does most of the collection concurrently with the execution of the application.

Concurent Mark Sweet Garbage collector is most widely used GC in java and it uses algorithm to first mark object which needs to be collected when GC triggers. It helps in increment Low Pause Collector

GC Tuning

Garbage Collector can be tuned depending upon various types of application charactersitcs, increasing performance of Java application. GC Tuning document provided by oracle provides a good overview on various cases of Garbage Collection.