Garbage Collection and Java

asraful

July 30, 2025

Originally Written : January 20, 2012

Introduction:

Garbage collection is an amazing feature of Java. Application developers use Java for various types of applications, ranging from small tools to complex enterprise software. Consequently, the demands on garbage collection vary based on the nature of the Java application. The Java HotSpotâ„¢ Virtual Machine implementation (Java HotSpotâ„¢ VM) provides multiple garbage collectors, each designed to satisfy different requirements.

The choice of garbage collector is not always a major concern; in most cases, it isn’t crucial. However, for large applications, it is tightly related to performance.

The performance of a Java-powered application is closely tied to the proper combination of three parameters:

  • Garbage collector
  • Heap size
  • Runtime compiler

Available Garbage Collectors:

The Java HotSpot VM includes three different collectors, each with distinct performance characteristics:

  • The serial collector
  • The parallel collector
  • The concurrent collector

Selecting a Garbage Collector:

  1. If the application has a small dataset (up to approximately 100MB), then the serial collector is acceptable. You can select it using the option: -XX:+UseSerialGC.
  2. If the application will be run on a single processor and there are no specific pause time requirements, then:
    • Let the VM select the collector, or
    • Select the serial collector with -XX:+UseSerialGC.
  3. If (a) peak application performance is the top priority and (b) there are no pause time requirements, or pauses of one second or longer are acceptable, then:
    • Let the VM select the collector, or
    • Select the parallel collector with -XX:+UseParallelGC and (optionally) enable parallel compaction with -XX:+UseParallelOldGC.
  4. If response time is more important than overall throughput, and garbage collection pauses must be kept shorter than approximately one second, then:
    • Select the concurrent collector with -XX:+UseConcMarkSweepGC. If only one or two processors are available, consider using incremental mode, as described below.