Garbage Collector Types


Every object that is created in java is stored in heap space.
The allocation and de-allocation of memory spaces for the objects is executed by automatic garbage collection process triggered by JVM.
Based on the flavor and version of JVM, different garbage collection algorithms are available in JVM, which can be specified via jvm parameters.
Java uses generational heap space where the referenced objects are transferred across different generations and garbage collection triggered based on heap space generational role which is termed as minor GC and major GC.

Types of Garbage Collectors

  • The Serial Garbage Collector.
  • The Parallel Garbage Collector.
  • The Concurrent Mark Sweep Garbage Collector.
  • The G1 Garbage Collector.

The Serial Garbage Collector.

As the name specifies here minor and major gcs are done serially using single CPU.
During the gc process all other application threads are paused.
This uses mark-compact-collection method, there by which moves existing object references to beginning of heap so that new memory allocations are done in single continuous chunk at the end of heap.


This is suitable where we have minimal memory and few cores.
This can also be suitable where more JVMs than the available cores are executed on same machine.
This being stop-of-world process and single-threaded may not be suitable for server environments which demands high-throughput/better responsiveness.

    Jvm arguments::

    -XX:+UseSerialGC - turns on serial garbage collector.

The Parallel Garbage Collector.

This is also called throughput collector.
This uses multiple threads for accelerating garbage collection process.
During the gc process all other application threads are paused.
Based on java version used there are two jvm arguments for parallel gc.
This process reduces the overall time spent by the application for garbage collection.


This is mostly useful for jvm process executing on multi core machines.
This collector is more suitable for batch jobs and more database intensive query operations.<

    Jvm arguments::

-XX:+UseParallelGC or -XX:+UseParallelOldGC  turns on parallel garbage collector.

Note: By default based on jvm version enabling one of the flag automatically enables the other flag unless it is disabled

The Concurrent Mark Sweep Garbage Collector.

This is also referred as concurrent low pause collector.
CMS uses the same parallel GC algorithm for younger generation objects.
This uses multiple threads to trace the reachable objects to identify objects for marking and removal.
The CMS garbage collector pauses the application threads during initial-marking (mark live objects reachable by gc roots) and re-mark (objects that become un-referenced after initial marking) phases.
All the remaining garbage collection process like tracing and sweeping of live objects run concurrently with application threads.
The CMS garbage collector uses more CPU resources than parallel garbage collector but with relatively lower pause times.


This is suitable for applications with large tenured generation objects and has more CPU resources.
This is required for applications which need higher responsiveness like web applications/services.

    Jvm arguments::

  -XX:+ UseConcMarkSweepGC - turns on CMS  garbage collector

The G1 Garbage Collector.

The G1 collector is parallel, concurrent and incrementally compacting low pause garbage collector.
This also operates concurrently together with application threads.
G1 collector does compaction of heap space on every cycle where as CMS does compaction only on stop the world process.
Other garbage collector uses fixed sizes for each generational heap space where as G1 uses different heap approach where heap is portioned into equal sized regions which are allocated to different generational roles based on demand, this approach provides better flexibility in heap allocation.
This heap distribution approach of G1 helps in having relatively lesser heap sizes.
G1 prioritizes its collection from the areas which has maximum reclaimable heap regions there by yielding maxing free space after gc.
G1 also has more predictable pause times as it can select the regions for gc which may fit into the configured pause interval.


This is mostly suitable for applications with has large heap and need predictable low gc pause times.

    Jvm arguments::

 -XX:+UseG1GC - turns on G1 garbage collector.


Each garbage collector can be fine tuned by configuring various jvm arguments which are specific to each gc collector type.
Java Ergonomics introduced in jdk 1.5, decides on default values for jvm parameters based on available system resources.
We can configure and fine tune these default values by overriding various jvm arguments, which will be mainly required when running multiple jvms on the same machine.

Also Read: