
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
Option |
Description |
-XX:+UseLargePages |
enable use large pages |
-XX:LargePageSizeInBytes |
large page size |
Misc
Print All Options
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.
PS Scavenge
Like the Copy collector, but uses multiple threads. (With PS Old Collector)
ParNew
Like the Copy collector, but uses multiple threads. (With Concurrent Old Collector)
G1
Old is also G1
.
Old Generation Collector
MarkSweepCompact
The serial mark-sweep collector, uses one thread full mark-sweep garbage collection algorithm, with optional compaction.
PS MarkSweep
The parallel scavenge mark-sweep collector, multiple threads version of MarkSweepCompact
.
ConcurrentMarkSweep
The CMS
collector.
G1 Mixed
The G1
collector.
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.
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
|
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>
|
Print Distribution
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
jinfo
Check the option value of a running JVM
1
| jinfo -flag <option> <pid>
|