JVM Garbage Collection Algorithms and Collectors
In Java, the primary function of Garbage Collection (GC) is to reclaim memory that is no longer used by the program.
Common garbage collection algorithms in Java include Mark-and-Sweep, Copying, Mark-Compact, and Generational Collection.
Mark-and-Sweep Algorithm
The Mark-and-Sweep algorithm is a fundamental garbage collection approach, consisting of two phases: marking and sweeping. During the marking phase, all objects eligible for reclamation are marked. In the sweeping phase, reclaimable objects are removed, and their occupied memory space is freed.
Since the Mark-and-Sweep algorithm does not rearrange available memory space after cleaning up the memory occupied by objects, if small objects make up a significant portion of reclaimable memory, it can lead to memory fragmentation. This fragmentation subsequently causes issues where large objects cannot obtain contiguous free space.
Copying Algorithm
The Copying algorithm was designed to address the memory fragmentation issue inherent in Mark-and-Sweep. It divides memory into two equal-sized regions, Region 1 and Region 2. Newly created objects are stored exclusively in Region 1. Once Region 1 is full, it undergoes a marking process; all surviving objects identified during this process are copied entirely to Region 2. At this point, Region 1 contains no living objects, allowing for the direct cleanup of its entire memory space.
The Copying algorithm offers high efficiency in memory cleanup and is straightforward to implement. However, since only one memory region is available at any given moment, usable memory is compressed to half its original capacity, resulting in significant memory waste. Furthermore, when a system contains a large number of long-lived objects, these objects must be copied back and forth between Region 1 and Region 2, which can negatively impact system performance.
Mark-Compact Algorithm
The Mark-Compact algorithm combines the advantages of both Mark-and-Sweep and Copying algorithms. Its marking phase is identical to that of Mark-and-Sweep. After marking is complete, surviving objects are compacted (moved) to one end of the memory space. The objects at the other end are then cleared, and their memory is freed.
Generational Collection Algorithm
Neither Mark-and-Sweep, Copying, nor Mark-Compact algorithms can effectively perform garbage collection on all types of objects. To address these challenges, the Generational Collection algorithm employs different garbage collection strategies for different object types within the JVM.
Garbage Collectors
The existence of garbage collectors liberates developers from the complex task of manually releasing memory, thereby improving development efficiency. Simultaneously, it shields developers from specific methods of memory release, preventing application crashes caused by incorrect memory operations and ensuring program stability.
The JVM provides various different garbage collectors tailored for both the Young Generation (New Generation) and the Old Generation. For the Young Generation, available collectors include Serial, ParNew, and Parallel Scavenge. For the Old Generation, options include Serial Old, Parallel Old, and CMS (Concurrent Mark Sweep). Additionally, there is the Garbage First partitioned collection algorithm designed for specific regions.
Serial Garbage Collector
The Serial garbage collector is implemented based on the Copying algorithm. It operates as a single-threaded collector; when performing garbage collection, it must pause all other working threads until the collection process is complete.
ParNew Garbage Collector
The ParNew garbage collector is also implemented based on the Copying algorithm but operates in a multi-threaded mode.
Parallel Scavenge Garbage Collector
The Parallel Scavenge garbage collector is similarly implemented based on the Copying algorithm and utilizes a multi-threaded working mode. It features significant optimizations for system throughput, allowing it to utilize CPU resources more efficiently to complete garbage collection tasks faster.
CMS Garbage Collector
CMS (Concurrent Mark Sweep) is implemented based on the Mark-and-Sweep algorithm and operates in a multi-threaded mode.
The CMS garbage collector is designed specifically for the Old Generation. Its primary objective is to achieve the shortest possible garbage collection pause time, thereby enhancing system stability in a multi-threaded environment by minimizing interruption duration.
Serial Old Garbage Collector
The Serial Old garbage collector is implemented based on the Mark-Compact algorithm and operates as a single-threaded process.
Parallel Old Garbage Collector
The Parallel Old garbage collector is implemented based on the Mark-Compact algorithm and utilizes a multi-threaded approach.
Garbage First Garbage Collector
The Garbage First garbage collector is implemented based on the Mark-Compact algorithm and operates in a multi-threaded mode.