Intro Java Garbage Collection Algorithms

Intro Java Garbage Collection Algorithms

這篇介紹Intro Java Garbage Collection Algorithms。

Java8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
default 使用 Parallel garbage collection
Simimar to serial GC, It uses mark-copy in the Young Generation and mark-sweep-compact
in the Old Generation. Multiple concurrent threads are used for marking and
copying / compacting phases.
You can configure the number of threads using -XX:ParallelGCThreads=N option.

Parallel Garbage Collector is suitable on multi-core machines in cases where your
primary goal is to increase throughput by efficient usage of existing system resources.
Using this approach, GC cycle times can be considerably reduced.

簡單來說,Parallel garbage collection通常用於較小的Java Heap,注重garbage collection的效率。

To use parallel GC, use below JVM argument:

-XX:+UseParallelGC

Java9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
default 使用 G1 garbage collection
The G1 (Garbage First) garbage collector was available in Java 7 and is designed
to be the long term replacement for the CMS collector. The G1 collector is a
parallel, concurrent, and incrementally compacting low-pause garbage collector.

This approach involves segmenting the memory heap into multiple small regions
(typically 2048). Each region is marked as either young generation (further
devided into eden regions or survivor regions) or old generation.
This allows the GC to avoid collecting the entire heap at once, and instead
approach the problem incrementally. It means that only a subset of the
regions is considered at a time.

簡單來說,G1 garbage collection通常用於較大的Java Heap,注重garbage collection的停頓時間。

If you want to use in Java 7 or Java 8 machines, use JVM argument as below:

-XX:+UseG1GC