JVM tuning - nimrody/knowledgebase GitHub Wiki
-
Displaying traces and overview of tracing methods: https://thume.ca/2023/12/02/tracing-methods/ (tracing / profiling)
-
Production options:
java -Xms3500M -Xmx3500M -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/ec2-user/policy.hprof -cp 'policy/*' MainWebServer config/qa.new.config dev.nimrod.tensera config logs
-
JVMtop (
https://github.com/patric-r/jvmtop/blob/master/doc/ConsoleProfiler.md
) -
Using
strace
to get system call statisticssudo strace -c -f -p
-
And to monitor specific system call such as
fsync
:sudo strace -e fsync -T -f -p
-
Histogram of heap
jmap -histo:live <pid>
Or remove the
:live
suffix to get all objects and avoid running a full GC -
Heap dump:
jmap -dump:live,format=b,file=/tmp/dump.hprof <pid>
(again, remove
:live
suffix to avoid full GC and get all objects)OR
jcmd 1 GC.heap_dump filename=dump.hprof
(add
-all
to get all objects and avoid a full GC) -
Exploring heap dump with
jhat
jmap -dump:file=eclipse.heap 16608 Dumping heap to C:\Program Files (x86)\Java\jdk1.6.0_11\bin\eclipse.heap ... Heap dump file created
jhat -J-Xmx1G eclipse.heap Reading from eclipse.heap... Dump file created Wed Oct 14 08:41:07 MSD 2009 Snapshot read, resolving... Resolving 2300585 objects... Chasing references, expect 460 dots............................................. ................................................................................ ................................................................................ ....................................... Eliminating duplicate references................................................ ................................................................................ ................................................................................ .................................... Snapshot resolved. Started HTTP server on port 7000 Server is ready.
And open a browser on localhost:7000
-
Monitoring garbage collection using jstat
jstat –gc <vmid> 1000
-
Asking the JVM to print GC stats:
-verbose:gc
-
Dump heap on OOM error
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/
-
Print all JVM flags and their values
java -XX:+PrintFlagsFinal
. Note the the following options- UseParallelGC
- UseParallelOldGc
- UseParallelNewGc
- UseConcurrentGC
- UseG1GC
(See here and here for more information on the above options)
-
Use
jcmd <pid-or-main-class-name> help
to get the list of available commandsjcmd ReportsWriterMain Thread.print # print all active threads jcmd ReportsWriterMain GC.heap_dump xx # dump heap to file xx in the directory where the jvm was started
- Use
jhat <filename>
to analyze heap dumps produced byjcmd GC.heap_dump
(orjvisualvm
for a gui)
-
Printing generated assembly (needs the hsdis-amd64 DLL available using
LD_LIBRARY_PATH
)java -XX:+UnlockDiagnosticVMOptions -XX:PrintAssemblyOptions=hsdis-print-bytes -XX:CompileCommand=print,MyClass.mymethod TestCode
-
Getting the number of context switches for a running process:
sudo perf stat -e cs -p
http://www.javamem.com/post.cfm/15-useful-performance-tools-for-java-environments
-
profiler4j [https://blog.idrsolutions.com/2014/06/java-performance-tuning-tools/profiler4j/] [https://www.openhub.net/p/profiler4j], [http://freecode.com/projects/profiler4j]
- To use
jmap
orjcmd
run it with the user that started the JVM. I.e.,sudo -u <jvm-user> jmap ...