Selecting a Proper Garbage Collector
Principles
The garbage collector is the implementation of memory reclamation. The garbage collector provided by the JDK has integrated the garbage collection and clearing algorithms. Service programs can select garbage collectors by setting parameters. The following table lists seven typical garbage collectors used by VMs. Based on the applicable memory area, the garbage collector of the JDK can be classified into the young-generation garbage collector and the old-generation garbage collector, which can be used together. The young-generation garbage collector is used for garbage collection in the young-generation area of the heap space, and the old-generation garbage collector is used for garbage collection in the young-generation region of the heap space. G1 is a new type of in-heap garbage collector, which can be used for both young- and old-generation garbage collection.
The following table lists the description and function classification of the seven typical garbage collectors:
Name |
Description |
Collection Mode |
Generation |
|---|---|---|---|
Serial |
Single-thread serial collector |
Serial collector |
Young generation |
ParNew |
multi-thread parallel serial collector |
Parallel collector |
Young generation |
Parallel Scavenge |
Parallel throughput priority collector |
Parallel collector |
Young generation |
Serial Old |
Single-thread serial collector for the old generation |
Serial collector |
Old generation |
Concurrent Mark Sweep (CMS) |
Parallel shortest pause duration collector |
Concurrent collector |
Old generation |
Parallel Old |
Parallel Scavenge for the old generation |
Parallel collector |
Old generation |
G1 |
New low-latency collector oriented to partial collection and region-based memory layout |
Concurrent/Parallel collector |
Young/Old generation |
The following figure shows how to use the young-generation GC and old-generation GC together. The GCs with connection lines can be used together. Note that ParNew and Parallel Old cannot be used at the same time.

There is no general criterion for selecting a garbage collector. Garbage collector selection should be based on the actual application of the project and the detection of GC running data.
Classic garbage collectors can be classified into three types based on the collection mode: serial collectors, parallel collectors, and concurrent collectors. Serial collectors are applicable only to scenarios with a small amount of data. The selection is mainly for parallel collectors and concurrent collectors. By default, JDK 1.5 uses serial collectors. If you want to use other collectors, you need to add the corresponding parameters during the startup. In JDK 1.5 and later versions, the JVM selects a garbage collector based on the current system configuration.
Suggestions on garbage collector selection:
- If service applications have high requirements on throughput and have no special requirements on response time, you are advised to use parallel collectors. For example: scientific computing and background processing programs.
- For medium- and large-sized applications that have high requirements on response time, you are advised to use concurrent collectors. For example: web servers.
- JDK 1.8 and later versions support multi-core processors have sufficient memory resources. The G1 collector is recommended.
- Single-thread applications use serial collectors.
Modification Method
The following table summarizes the types, characteristics, and modification parameters of the various collectors:
Name |
Modification Parameter |
Feature |
|---|---|---|
Serial |
-XX:+UseSerialGC |
Single-thread collector for the young generation. The replication algorithm is used for garbage collection. When Serial collects garbage, all user threads must be stopped (Stop The World). |
ParNew |
-XX:+UseParNewGC |
Multi-thread version of Serial. It is not better than Serial in the single-core CPU environment. Collecting the number of threads and CPU cores is enabled by default. You can set the number of threads for garbage collection through the -XX:ParallelGCThreads parameter. |
Parallel Scavenge |
-XX:+UseParallelGC For JDK 1.7 and 1.8, the young-generation garbage collector is used by default. |
Multi-thread collector for the young generation. ParNew aims to minimize the pause duration of user threads during garbage collection, and Parallel Scavenge aims to achieve a controllable throughput. The -XX:MaxGCPauseMillis parameter is used to set the shortest memory reclamation time. The -XX:GCTimeRatio parameter is used to accurately control the throughput. |
Serial Old |
-XX:+UseSerialOldGC |
Serial for the old generation. The mark-compact algorithm is used to collect single-thread garbage. |
CMS |
-XX:+UseConMarkSweepGC |
A collector with the shortest collection pause duration as the target. CMS is based on the mark-sweep algorithm. Therefore, fragments are generated after garbage collection. You can use the -XX:UseCMSCompactAtFullCollection parameter to enable defragmentation (enabled by default). The -XX:CMSFullGCsBeforeCompaction parameter is used to set the number of full GCs without compression (defragmentation is not performed) before a full GC with compression (defragmentation is performed). The -XX:ParallelCMSThreads parameter specifies the number of CMS threads. |
Parallel Old |
-XX:+UseParallelOldGC For JDK 1.7 and 1.8, the old-generation garbage collector is used by default. |
Parallel Scavenge for the old generation. The -XX:ParallelGCThreads parameter is used to limit the number of threads. |
G1 |
-XX:+UseG1GC This parameter is provided in JDK 1.7 and later versions, and used in JDK 1.9 by default. |
A new collector that supports both parallel and concurrent processing and makes full use of multi-CPU resources. No memory fragments are generated during the running of the collector. The -XX:ParallelGCThreads parameter is used to limit the number of threads. The -XX:MaxGCPauseMillis parameter specifies the maximum pause duration. |