JVM Options

(updated: )
  1. 1. Options
    1. 1.1. Sizing
    2. 1.2. Performance Options
    3. 1.3. Misc
      1. 1.3.1. Print All Options
      2. 1.3.2. Enable GC Logs
      3. 1.3.3. Alter DNS Cache
      4. 1.3.4. Do Not Wait Antropy For SecurityRandom
      5. 1.3.5. Dump on OOM
      6. 1.3.6. Script on OOM
      7. 1.3.7. Enable JMX
  2. 2. Carbage Collector
    1. 2.1. Young Generation Collector
      1. 2.1.1. Copy
      2. 2.1.2. PS Scavenge
      3. 2.1.3. ParNew
      4. 2.1.4. G1
    2. 2.2. Old Generation Collector
      1. 2.2.1. MarkSweepCompact
      2. 2.2.2. PS MarkSweep
      3. 2.2.3. ConcurrentMarkSweep
      4. 2.2.4. G1 Mixed
    3. 2.3. Combinations
    4. 2.4. Example
      1. 2.4.1. Parallel Collector
      2. 2.4.2. CMS Collector
      3. 2.4.3. Phases
      4. 2.4.4. G1 Collector
  3. 3. Tools
    1. 3.1. jmap
      1. 3.1.1. Dump The Heap
      2. 3.1.2. Dump The Heap, Live Only
      3. 3.1.3. Print Distribution
    2. 3.2. jstat
      1. 3.2.1. Loaded/Unloaded Classes Info
      2. 3.2.2. Compiler Info
      3. 3.2.3. Capacities (KB) and GC Info
      4. 3.2.4. Capacities (Ratio) and GC Info
      5. 3.2.5. Capacities (KB) Info
    3. 3.3. jstack
    4. 3.4. jinfo

Overview

Options

Sizing

Option Description Example
-Xms min heap size -Xms8G
-Xmx max heap size -Xmx8G
-Xmn min and max young size -Xmn2G
-Xss thread stack size, equivalent to -XX:ThreadStackSize -Xss1M
–XX:NewRatio young size = heap/(NewRatio + 1), or NewRatio = old / young default: –XX:NewRatio=2
-XX:NewSize min young size -XX:NewSize=3G
-XX:MaxNewSize max young size -XX:MaxNewSize=3G
-XX:SurvivorRatio each survivor size = young/(SurvivorRatio + 2), or SurvivorRatio = eden / each survivor Default: -XX:SurvivorRatio=8
-XX:PermSize min PermGen size, Java < 8 -XX:PermSize=512M
-XX:MaxPermSize max PermGen size, Java < 8 -XX:PermSize=512M
-XX:MaxMetaspaceSize max metaspace size, Java >= 8 -XX:MaxMetaspaceSize=1G
-XX:ThreadStackSize thread stack size, equivalent to -Xss -XX:ThreadStackSize=1M

Notice

  • Set -Xms and -Xmx the same value to avoid heap resizing in production

Performance Options

Option Description
-XX:+UseLargePages enable use large pages
-XX:LargePageSizeInBytes large page size

Misc

1
java -server -XX:+AggressiveOpts -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+PrintFlagsFinal -version

Enable GC Logs

1
2
3
4
5
6
7
8
9
10
11
-verbose:gc
-XX:+PrintGCApplicationStoppedTime
-XX:+PrintGCDateStamps
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-XX:+PrintHeapAtGC
-XX:+PrintTenuringDistribution
-Xloggc:../logs/gc-`date +%F_%H-%M-%S`.log
-XX:+UseGCLogFileRotation
-XX:NumberOfGCLogFiles=20
-XX:GCLogFileSize=200M

Alter DNS Cache

1
-Dsun.net.inetaddr.ttl=<TTL in seconds>

Do Not Wait Antropy For SecurityRandom

1
-Djava.security.egd=file:/dev/./urandom

Dump on OOM

1
2
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=../logs/dump-`date +%F_%H-%M-%S`.hprof

Script on OOM

1
-XX:OnOutOfMemoryError="/path/to/script.sh

Enable JMX

1
2
3
4
-Djava.rmi.server.hostname=<external IP>
-Dcom.sun.management.jmxremote.port=<port>
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

Carbage Collector

Young Generation Collector

Copy

Uses one thread to copy surviving objects from Eden to Survivor spaces and between Survivor spaces.

1
-XX:+UseSerialGC

PS Scavenge

Like the Copy collector, but uses multiple threads. (With PS Old Collector)

1
-XX:+UseParallelGC

ParNew

Like the Copy collector, but uses multiple threads. (With Concurrent Old Collector)

1
-XX:+UseParNewGC

G1

Old is also G1.

1
-XX:+UseG1GC

Old Generation Collector

MarkSweepCompact

The serial mark-sweep collector, uses one thread full mark-sweep garbage collection algorithm, with optional compaction.

1
-XX:+UseSerialGC

PS MarkSweep

The parallel scavenge mark-sweep collector, multiple threads version of MarkSweepCompact.

1
-XX:+UseParallelOldGC

ConcurrentMarkSweep

The CMS collector.

1
-XX:+UseConcMarkSweepGC

G1 Mixed

The G1 collector.

1
-XX:+UseG1GC

Combinations

Option Young Old
-XX:+UseSerialGC Copy MarkSweepCompact
-XX:+UseG1GC G1 Young G1 Mixed
-XX:+UseParallelGC PS Scavenge PS MarkSweep
-XX:+UseConcMarkSweepGC ParNew MarkSweepCompact

Example

Parallel Collector

For backend application, for its high throughput.

1
-server -Xms4G -Xmx4G

CMS Collector

For frontend application, for its low latency.

1
2
3
4
-server -Xms6G -Xmx6G -Xmn2G -XX:SurvivorRatio=2
-XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly
-XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark -XX:CMSInitiatingOccupancyFraction=65
-XX:+ParallelRefProcEnabled -XX:+AlwaysPreTouch

Phases

  • Initial Mark: stop-the-world, identify direct reachable objects from GC roots
  • Concurrent Mark: mark the objects reachable from initially marked
  • Precleaning: mark new objects as live/dead in the OOP table
  • Remark: stop-the-world, retrace from GC roots and remark objects as live/dead in the OOP table
  • Concurent Sweep: mark dead objects as free space
  • Reset: reset the state to get ready for next collection

OOP = Ordinary Object Pointer

G1 Collector

For applications that use big memorys (at least 8G), or you want to play it safe.

1
-server -Xms12G -Xmx12G -XX:+UseG1GC -XX:MaxGCPauseMillis=100

Tools

jmap

Dump The Heap

1
jmap -dump:format=b,file=dump.hprof <pid>

Dump The Heap, Live Only

1
jmap -dump:live,format=b,file=dump.hprof <pid>
1
jmap -heap <pid>

jstat

Loaded/Unloaded Classes Info

1
jstat –class [-h15] <pid> [interval] [count]

Compiler Info

1
jstat -compiler [-h15] <pid> [interval] [count]

Capacities (KB) and GC Info

1
jstat -gc [-h15] <pid> [interval] [count]

Capacities (Ratio) and GC Info

1
jstat -gcutil [-h15] <pid> [interval] [count]

Capacities (KB) Info

1
jstat -gccapacity [-h15] <pid> [interval] [count]
Column Description
S0C Survivor0 space capacity (KB)
S1C Survivor1 space capacity (KB)
S0U Survivor0 space utilization (KB)
S1U Survivor1 space utilization (KB)
EC Eden space capacity (KB)
EU Eden space utilization (KB)
OC Old space capacity (KB)
OU Old space utilization (KB)
PC Permanent space capacity (KB)
PU Permanent space utilization (KB)
YGC number of Yong GC events
YGCT used time for Yong GC
FGC number of Full GC events
FGCT used time for Full GC
GCT total used time for GC
MC Metaspace capacity (KB)
MU Metacspace utilization (KB)
CCSC Compressed class space capacity (KB)
CCSU Compressed class space used (KB)
NGCMN Young space min capacity (KB)
NGCMX Young space max capacity (KB)
NGC Young space current capacity (KB)
OGCMN Old space min capacity (KB)
OGCMX Old space max capacity (KB)
OGC Old space current capacity (KB)

jstack

Print threads information

1
jstack <pid>

jinfo

Check the option value of a running JVM

1
jinfo -flag <option> <pid>