Skip to content

Instantly share code, notes, and snippets.

@jtviegas
Last active August 25, 2020 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtviegas/d2267086e3ceb3bb51560f712db04f4a to your computer and use it in GitHub Desktop.
Save jtviegas/d2267086e3ceb3bb51560f712db04f4a to your computer and use it in GitHub Desktop.
java vm options

the vm

get the vm flags

-XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal

check just in time compilation

levels: 1,2,3 (C1 compiler)

level: 4 (C2 compiler => native code) (not on 32 bit vm)

-XX:+PrintCompilation

save compilation log in remote jvm

-XX:+UnlockDiagnosticVMOptions -XX:+LogCompilation

check size of code cache

-XX:+PrintCodeCache

set code cache initial size

-XX:InitialCodeCacheSize=120m

set code cache max size

-XX:ReservedCodeCacheSize=240m

set code cache expansion size

-XX:CodeCacheExpansionSize=16m

turn off tiered compilation (C2) - for one off procedure apps

-XX:-TieredCompilation

native compilation tuning

get flags defaults

java -XX:+PrintFlagsFinal

set compiler thread count

java -XX:CICompilerCount=4

check compiler thread count of running pid

jinfo -flag CICompilerCount 11280

num of times a method should run before being natively compiled

jinfo -flag CompileThreshold 11280

set method compile threshold

java -XX:CompileThreshold=5000

memory notes

stack | heap | metaspace

metaspace (permgen before java 8)

  • general metadata: info about classes, methods, which methods were compiled into bytecode vs native;
  • static primitives are entirely stored there whereas static objects are stored in the heap but the pointer is stored in the metaspace;
  • variables in the metaspace are never garbage collected

interning strings

Integer i = 98;
String s = i.toString().intern();

the string pool

print statistics

-XX:+PrintStringTableStatistics

set table size - always prime number

-XX:StringTableSize=120121

heap

get the flags

-XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal

set the max heap size

-XX:MaxHeapSize=2g -XX:InitialHeapSize=1g == -Xmx2g -Xms1g

heap dump

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heap.dump

garbage collection

generational gc: young generation vs old generation

young generation heap <= new objects

  • garbage collection starts at the young generation (YG)
  • minor and multiple gc's and mostly full of garbage
  • objects surviving gc in YG are moved to old generation (OG)
  • it's divided in 3 sections:
    • Eden - new objects are created here, when full gc kicks in
    • s0 - (survive space - used to swap) - after some generations objects are moved to OG
    • s1 - (survive space - used to swap) - after some generations objects are moved to OG

old generation heap

  • when gets full, gc kicks in and normally it's a major gc

tuning and selection

add verbosity to gc

-verbose:gc

disable YG memory resize

-XX:-UseAdaptiveSizePolicy

improve GC

check size ratio OG/YG

jinfo -flag NewRatio 10236

decrease size ratio OG/YG (1 is the smaller ratio)

  • this will increase the size of YG and decrease the number of GC's in OG (desirably) -XX:NewRatio=1

check and decrease the size ratio YG/Survivor(s0+s1)

jinfo -flag SurvivorRatio 10236 -XX:SurvivorRatio=1

how many generations should an object survive before it is part of old generation

-XX:MaxTenuringThreshold=1 # max 15 -> everything in new generation

choosing a GC

  • serial -XX:+UseSerialGC
  • parallel -XX:+UseParallelGC # default in java 8
  • mostly concurrent -XX:+UseConcMarkSweepGC or -XX:+UseG1GC # older java versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment