Java Performance Toolbox
- Cyanny Liang
I learned The Java Performance Definitive Guide[chapter 3] on this weekend, here is a brief summary about Java Performance Toolbox.
vmstat: Report virtual memory statistics, vmstat reports information about processes, memory, paging, block IO, traps, disks and cpu activity
vmstat [options] [delay [count]]
- CPU time is the first thing to examine when looking at performance of an application.
- The goal in optimizing code is to drive the CPU usage up (for a shorter period of time), not down.
- Understand why CPU usage is low before diving in and attempting to tune an application.
iostat: Report Central Processing Unit (CPU) statistics and input/output statistics for devices and partitions.
- %user: Show the percentage of CPU utilization that occurred while executing at the user level (application).
- %system: Show the percentage of CPU utilization that occurred while executing at the system level (kernel).
- rrqm/s: The number of read requests merged per second that were queued to the device
- avgrq-sz: The average size (in sectors) of the requests that were issued to the device
- %util: Percentage of elapsed time during which I/O requests were issued to the device (bandwidth utilization for the device). Device saturation occurs when this value is close to 100%
iostat -xm 5
avg-cpu: %user %nice %system %iowait %steal %idle
9.40 0.00 0.52 0.01 0.00 90.07
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.58 0.15 4.49 0.00 0.14 61.65 1.31 283.55 0.22 292.90 0.06 0.03
netstat: Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
The book use
nicstat, which is not built-in Linux server, it can be installed by yum.
Be careful that the bandwidth is measured in bits per second, but tools generally report bytes per second
jcmd process_id VM.uptime
Some tuning flags can be set by jcmd and jinfo in command line, such as manageable options and C2 diagnostic (the flag provides diagnostic output for the compiler engineers to understand how the compiler is functioning).
jinfo -flag -PrintGCDetails process_id # turns off PrintGCDetails
- jcmd can be used to find the basic VM information—include the value of all the tuning flags—for a running application.
- Default flag values can be found by including -XX:+PrintFlagsFinal on a command line. This is useful for determining the default ergonomic settings of flags on a particular platform.
- jinfo is useful for inspecting (and in some cases changing) individual flags.
jmap -heap process_id
Another way, add hprof option to java process
-agentlib:hprof=cpu=samples,lineno=y # for cpu
- heap=sites, sites is a sorted list of allocation sites. This identifies the most heavily allocated object types, and the TRACE at which those allocations occurred.
- cpu=samples, is a statistical profile of program execution. The VM periodically samples all running threads, and assigns a quantum to active TRACEs in those threads.
- cpu=time, is a profile of program execution obtained by measuring the time spent in individual methods (excluding the time spent in callees), as well as by counting the number of times each method is called
Heap dumps can be captured from the jvisualvm GUI, or from the command line using jcmd or jmap.
Or you can use Eclipse Memory Analzyer Tool.
Sampling-based profilers are the most common profiler. There may be error in sampling profiler’s result. The way to minimize these errors is to profile over a longer period of time, and to reduce the time interval between samples.
Instrumented profilers yield more information about an application, but can possibly have a greater effect on the application than a sampling profiler.
Instrumented profilers should be set up to instrument small sections of the code, a few classes or packages. That limits their impact on the application’s performance.
It’s a great profiling tool built-in JDK(jdk 7 or higher). On local machine, just type
jmc command, the jmc UI will show.
Then how to connect jmc to remote Linux server.
- Firstly, add jmx configurations to Linux java process
-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=8091 -Dcom.sun.management.jmxremote.rmi.port=8091 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false
- Secondly, config local jmc connection
fill the server and port, click Finished. That’s all.
Use JMC the dump files:
-XX:+UnlockCommercialFeatures -XX:+FlightRecorder to application
secondly, type these commands:
jcmd process_id JFR.start
- System monitoring tools: vmstat, iostat, netstat
- Java built-in tools: jinfo, jcmd, jmap, jhat, jstat, jconsole, jvisualvm, jmc, jhprof
- No perfert tools for everything, when do profiling work, use right tools right applications