Garbage Collection Algorithms in Java
What is Garbage Collection ?
Java provides Garbage Collection feature to remove the objects from memory when the objects are no longer in use. The JVM finds the objects that no longer have any references to them and removes them from heap memory. Java used special objects called Garbage Collection Roots (GC roots) which act as a root objects (local variables, active threads, static variables, & JNI references) using which objects are tracked for garbage collection.
Below are few examples that shows how a Car or Engine class object becomes eligible for GC once its value is set to null.
Car car = new Car(); car = null;
Car car = new Car(); Engine engine = new Engine(); car.setEngine(engine); engine.setCar(car) engine = null; car = null;
Minor GC (Garbage Collection)
Heap is divided into old and young generations. The young generation is further divided into eden and survivor spaces. When the young generation fills up, garbage collector will stop all the application threads and cleanup young generation. Objects that are no longer in use are discarded. This operation is called minor GC.
Full GC (Garbage Collection)
As objects are moved from young to old/tenured generation, it also gets filled up. Now JVM starts looking for objects that are no longer in use. The GC algorithms like serial and throughput garbage collector stops all the application threads, find the unused objects, and free up memory. This process is called full GC.
GC (Garbage Collection) Algorithms
The JVM provides four algorithms to perform GC.
1) Serial Garbage collector
Serial Garbage collector is the default garbage collector for the applications running on a client-class machine (typically 32-bit JVM on windows or single-processor machines). Serial Garbage collector uses a single thread to process the heap. It will stop all the application threads as the heap is processed for minor/full GC. Serial Garbage collector can be enabled by using -XX:+UseSerialGC flag.
2) Throughput Garbage collector
Throughput Garbage collector is the default garbage collector for the applications running on a server-class machine (typically 64-bit JVM on windows or multi-CPU Unix machines). Throughput Garbage collector uses multiple threads to process the heap. It will stop all the application threads as the heap is processed for both minor GC & full GC. Throughput Garbage collector can be enabled by using -XX:+UseParallelGC flag.
3) CMS Garbage collector
CMS Garbage collector uses multiple threads to process the heap, but it will stop all the application threads only during minor GC & not during full GC. CMS Garbage collector use separate threads to periodically process old generation, thus avoids stopping application pause during a full GC. CMS Garbage collector can be enabled by using -XX:+UseConcMarkSweepGC flag.
4) G1 Garbage collector
G1 Garbage collector is designed to process large heaps (greater than 4 GB). G1 Garbage collector also uses multiple threads to process the heap, and it will stop all the application threads only during minor GC & not during full GC. G1 Garbage collector use separate threads to periodically process old generation, thus avoids stopping application pause during a full GC. G1 Garbage collector can be enabled by using -XX:+UseG1GC flag.
References :
Java Garbage Collection