JVM Garbage Collectors

Last Updated : 30 Sep, 2026

JVM Garbage Collectors automatically manage memory by identifying and reclaiming heap space occupied by objects that are no longer reachable. This helps the JVM use available memory efficiently during program execution.

  • Automatically reclaims memory from unreachable objects.
  • Helps manage heap memory without requiring manual memory deallocation.
Java
public class GFG {
    public static void main(String[] args) {
        String data = new String("Garbage Collection Demo");
        
        // Object becomes eligible for garbage collection
        data = null; 
    }
}

Explanation: When the reference is set to null, the object is no longer reachable and becomes eligible for garbage collection.

Threads in Garbage Collection

1. Controlling Garbage Collection Threads

To control the number of threads used by the garbage collector, we can use multi-threaded collectors like the Parallel Garbage Collector.

Implementation:

java -XX:+UseParallelGC -XX:ParallelGCThreads=NumberOfThreads -jar ApplicationJar.java

2. Limiting GC Pause Time

To limit the maximum pause time during garbage collection, we can use the MaxGCPauseMillis option:

Implementation:

java -XX:+UseParallelGC -XX:MaxGCPauseMillis=SecInMillisecond -jar ApplicationJar.java

Types of Garbage Collectors

1. Serial Garbage Collector

The Serial Garbage Collector is the simplest GC and uses a single thread for all garbage collection tasks. It pauses all application threads during GC and is best suited for small applications or systems with limited memory.

Implementation

java -XX:+UseSerialGC -jar ApplicationJar.java

2. Parallel Garbage Collector

The Parallel Garbage Collector (default in Java 8) uses multiple threads to perform garbage collection, improving throughput. It pauses the application during GC but provides better performance than Serial GC and is ideal for high-throughput applications.

Implementation:

java -XX:+UseParallelGC -jar ApplicationJar.java

3. Concurrent Mark-Sweep Collector

The Concurrent Mark-Sweep (CMS) collector was designed to reduce garbage-collection pause times by performing much of its work concurrently with application threads. It was primarily intended for applications that required shorter pause times.

Note: CMS is a legacy garbage collector and has been removed from modern JDK releases. Options such as -XX:+UseConcMarkSweepGC and -XX:+UseParNewGC should not be presented as current JVM options.

4. G1 Garbage Collector

G1 GC divides the heap into multiple regions and collects regions with the most garbage first. It provides predictable pause times, supports large heaps, and became the default GC in Java 9.

Implementation:

java -XX:+UseG1GC -jar ApplicationJar.java

5. Z Garbage Collector

ZGC is a low-latency garbage collector introduced in Java 11. It handles very large heaps with pause times typically in milliseconds, making it suitable for performance-critical applications.

Implementation:

java -XX:+UseZGC -jar ApplicationJar.jar

6. Shenandoah Garbage Collector

Shenandoah, introduced in Java 12, focuses on very low pause times by running garbage collection concurrently with application threads. It is ideal for latency-sensitive systems.

Implementation:

java -XX:+UseShenandoahGC -jar ApplicationJar.jar

Garbage Collection JVM Options

OptionDescription
-Xms<size>Sets the initial heap size.
-Xmx<size>Sets the maximum heap size.
-XX:MaxMetaspaceSize=<size>Sets the maximum amount of native memory that can be used for class metadata in Metaspace.
-XX:+HeapDumpOnOutOfMemoryErrorGenerates a heap dump when an OutOfMemoryError occurs.
-XX:HeapDumpPath=<path>Specifies the location where the heap dump is saved.
-Xlog:gcEnables basic garbage-collection logging.
-Xlog:gc*Enables more detailed garbage-collection logging.
-XX:+UseG1GCEnables the G1 Garbage Collector.
-XX:+UseContainerSupportEnables JVM support for detecting and using container resource limits on supported platforms.

Garbage Collector Options

Here's a table describing the available garbage collections algorithms and optimization options in the JVM.

OptionDescription
-XX:+UseSerialGCEnables the Serial Garbage Collector.
-XX:+UseParallelGCEnables the Parallel Garbage Collector.
-XX:+UseG1GCEnables the G1 Garbage Collector.
-XX:+UseZGCEnables ZGC.
-XX:+UseShenandoahGCEnables the Shenandoah Garbage Collector, when supported by the JDK build.
-XX:ParallelGCThreads=<n>Configures parallel GC worker threads for applicable collectors.
-XX:G1HeapRegionSize=<size>Configures the G1 heap region size.
Comment