5. 5
Java GC: A Performance Impact Garbage Collection Overview
GC Purpose
Objects that are referenced are said to be live
Objects that are no longer referenced are considered dead and termed garbage
Garbage collector is responsible for:
allocating memory
ensuring that any referenced objects remain in memory
recovering memory used by objects that are no longer reachable from
references in executing code
7 November 2014
6. 6
Java GC: A Performance Impact Garbage Collection Overview
GC Purpose
7 November 2014
7. 7
Java GC: A Performance Impact Garbage Collection Overview
GC performance impact
Collector needs computational resources (CPU cycles) to perform garbage
collection
As garbage collection involves moving objects in memory a collector must
ensure that no thread is using these objects
7 November 2014
The pauses when all application threads are stopped are called
stop-the-world pauses
These pauses generally have the greatest impact on the performance of an
application, and minimizing those pauses is the key consideration when tuning
GC.
8. 8
Java GC: A Performance Impact Garbage Collection Overview
Generational model
7 November 2014
9. 9
Java GC: A Performance Impact Garbage Collection Overview
Generational model
7 November 2014
10. 10
Java GC: A Performance Impact Garbage Collection Overview
Generational model
7 November 2014
11. 11
Java GC: A Performance Impact Garbage Collection Overview
Generational model
7 November 2014
12. 12
Java GC: A Performance Impact Garbage Collection Overview
Generational model
7 November 2014
13. 13
Java GC: A Performance Impact Garbage Collection Overview
Generational model
7 November 2014
14. 14
Java GC: A Performance Impact Garbage Collection Overview
Summary
all GC algorithms divide the heap into old and young generation
all GC algorithm employ stop-the-world approach to clearing objects from young
generation, which is usually a very quick operation
7 November 2014
16. 16
Java GC: A Performance Impact Java GC Algorithms
“Client” and “Server” JVM types
7 November 2014
Depending on underlying hardware platform and version JVM can act as
“client” of “server” VM. This affect the choice of JIT compiler and default GC
algorithm.
“client” platform is usually 32-bit and has 1 CPU
“server” platform is usually 64-bit (but 32-bit is also possible) and has several
CPUs
18. default collector for client-class platforms (32-bit JVMs on Windows or single-processor
18
Java GC: A Performance Impact Java GC Algorithms
Serial garbage collector
7 November 2014
machines)
uses single thread to process heap
stops all application threads for both minor and full GC
Usage cases:
no low-pause requirements
“client-style” single-CPU environment
very small heap (few hundred MBs)
several JVMs running on single platform (number of JVM > number of available
CPUs)
19. default collector for server-class machines (multi-CPU Unix machines or any 64-
utilizes multiple threads for garbage collection to gain speed and minimize
stops all application threads for both minor and full GC
-XX:+UseParallelGC enables multi-threaded collection of young generation and
-XX:+UseParallelOldGC enables multi-threaded collection of young generation
19
Java GC: A Performance Impact Java GC Algorithms
Throughput (parallel) garbage collector
bit JVM)
pauses
single-threaded old-generation collection/compaction
and multi-threaded old-generation collection/compaction
Usage cases:
multi-CPU are available
large heap size and many object created/discarded
7 November 2014
20. designed to eliminate long pauses associated with full GC cycles
stops all application threads during minor GC
uses different algorithm to collect young generation (-XX:+UseParNewGC)
uses one or more background threads to periodically scan through the old
do not perform any compaction
in case of CPU unavailability and/or heap fragmentation – fallback to serial
low pause requirement and available CPU resources
in case of single-CPU machine can be used with -XX:+CMSIncrementalMode
20
Java GC: A Performance Impact Java GC Algorithms
CMS (Concurrent Mark Sweep) collector
generation and discard unused objects. This makes CMS a low-paused collector
collector
by default does not collect permgen
Usage cases:
(deprecated in Java 8)
7 November 2014
21. designed to process large heaps (more than 4 Gb) with minimal pauses
divides the heap into separate regions
performs incremental compaction of old generation by copying data between
21
Java GC: A Performance Impact Java GC Algorithms
G1 (Garbage First) garbage collector
regions
7 November 2014
22. Serial GC is best only for application with heap <= 100 Mb
Batch jobs which consume all available CPUs will get better performance with
Batch jobs which DON’T consume all CPUs could get better performance with
When measuring response time the choice between throughput and concurrent
Most of the time CMS should overperform G1 for heaps < 4 Gb
For large heaps G1 is better because of the way it can divide work between
22
Java GC: A Performance Impact Java GC Algorithms
Summary (choosing GC algorithm)
concurrent collector
throughput collector
collectors depends on CPU availability
different threads and heap regions
7 November 2014
24. Too small heap -> too much time spent in GC
Main rule – never to specify heap more than the amount of available physical
24
Java GC: A Performance Impact Basic GC Tuning
Sizing the heap
memory
-Xms – initial heap size
-Xmx – maximum heap size
7 November 2014
25. 25
Java GC: A Performance Impact Basic GC Tuning
Sizing the Generations
-XX:NewRatio=N – sets the ration of young generation to old
-XX:NewSize=N – sets the size of young generaion
-XX:MaxNewSize=N – maximum size for young generation
7 November 2014
26. 26
Java GC: A Performance Impact Basic GC Tuning
Sizing Permgen and Metaspace
Java 7:
-XX:PermSize=N
-XX:MaxPermSize=N
Java 8:
-XX:MetaspaceSize=N
-XX:MaxMetaspaceSize=N
7 November 2014
27. Adaptive sizing controls how the JVM alters the ratio of young generation to old
Adjusting generation sizes is base on GC algorithms attempts to meet their
Adaptive tuning can be disabled for small performance boost (usually not
Command line argument differs for different GC algorithms. For, example for
-XX:GCTimeRatio=nnn - hint to the virtual machine that it's desirable that not more
27
Java GC: A Performance Impact Basic GC Tuning
Adaptive Sizing
JVM can try to find optimal performance according to its policies and configuration
pause goals
recommended)
throughput collector:
-XX:+UseAdaptiveSizePolicy – whether to use adaptive policy (true by default)
-XX:MaxGCPauseMillis=nnn – maximal GC pause we can tolerate
than 1 / (1 + nnn) of the application execution time be spent in the collector
7 November 2014