Friday 3 July 2015

Garbage Collector in Java

     
Garbage Collector is very vast topic, but I am going to cover only here what every Java Developer must know.

What is Garbage Collection?

• Garbage Collection is a process in java which is carried by a daemon thread called “Garbage Collector (GC).”
• Garbage Collector will call finalize method of object before removing it from memory. Thus it will allow us to enable clean up processing.
• We can't force Garbage Collector to run it at the moment, but we can just request it using System.gc() or Runtime.gc().
• Garbage collector types are defined based on how it works rather than which type of memory it collects.
• All types of collectors can collect the all the types of memory.
• There are four types of garbage collectors available in JVM, each of them have its own unique advantages and disadvantages. The choice of which one to use isn’t automatic and lies on your shoulders and the it depends on differences in throughput and application pauses.

Types of Garbage Collectors

1. The Serial Collector

• Designed for single-threaded environments (e.g. 32 bit or Windows) and for small heaps.
• It freezes all application threads whenever it’s working. (Application Pauses)
• It uses just a single thread for garbage collection.
• Almost outdated now.

2. The Parallel/Throughput Collector

• Designed for multi-threaded environments.
• It also freezes all application threads whenever it’s working. (Application Pauses)
• Uses multiple threads for garbage collection.
• It is the JVM’s default collector.

3. The CMS(Concurrent Mark Sweep) Garbage Collector

• Designed for multi-threaded environments.
• It will freezes all application threads, in two scenarios only.
  ➤ While marking the referenced objects in the tenured/old generation space
  ➤ If there is a change in heap memory in parallel while doing the garbage collection.
• It uses multiple threads (“concurrent”) to scan through the heap (“mark”) for unused objects that can be recycled (“sweep”).
• It ensure better application throughput.
• It's not by default Collector used by JVM.
• It uses more CPU in compare to the Parallel Collector.
• Race condition occurs between collecting the young and old generations.
• If you are willing to allocate more CPU resources (multicore processors) to avoid application pauses this is the collector you’ll probably want to use.
• It's suitable when your heap is less than 4Gb in size.

4. The G1 (Garbage First) collector

• The Garbage first collector (G1) introduced in JDK 7 update 4.
• The collector splits the heap up into fixed-size regions and tracks the live data in those regions (spanning from 1MB to 32MB (depending on the size of your heap)).
• The G1 collector utilizes multiple background threads to scan through the heap (which divided into regions).
• When a GC is deemed necessary, it collects the regions with less live data first (hence, "garbage first").
• It's suitable when your heap is greater than 4Gb in size.

How to apply?

• Serial generational collector ➤ -XX:+UseSerialGC
• Parallel for young space, Serial for old space generational collector ➤ -XX:+UseParallelGC
• Parallel for young and old both spaces generational collector ➤ -XX:+UseParallelOldGC
• Concurrent mark sweep with serial young space collector ➤ -XX:+UseConcMarkSweepGC –XX:-UseParNewGC
• Concurrent mark sweep with parallel young space collector ➤ -XX:+UseConcMarkSweepGC –XX:+UseParNewGC
• G1 garbage collector ➤ -XX:+UseG1GC

No comments :