The JDK itself provides a wealth of performance monitoring tools, in addition to the integrated VISUALVM and Jconsole, as well as jstat,jstack,jps,jmap,jhat gadgets, which are common tools for performance tuning
In the development of Java, we often encounter the following problems:
1 out of memory, 2 memory leaks, 3 lock contention, 4 wire locks dead, 5java process consumes high CPU
When these problems arise, we often resolve them by restarting the server or by adjusting the memory.
One, JPS command
JPS is primarily used to output process state information running in the JVM
Command line parameter options:
-Q does not output the class name, jar name, and parameters passed in the Main method
-m output parameters that pass in the Main method
-L OUTPUT The full name of the main class or Jar
-V Output parameters for incoming JVM
The most common is jps-l.
[[email protected] bin]# jps-l1217 sun.tools.jps.Jps1094 Org.apache.catalina.startup.Bootstrap can also be viewed via Ps-ef|grep Java All Java application processes
Two Jstat command
Jstat It can display the class load, memory, garbage collection, JIT compiling and other running data in the virtual machine process.
Class 1 Load Statistics:
[[email protected] bin]# jstat-class 1094Loaded Bytes Unloaded Bytes time 3133 6273.2 0 0.0 1.91 ======================loaded: Number of loaded Classes bytes: Amount of space occupied unloaded: Quantity not loaded bytes: Footprint not loaded time: ==================== ==
2 Compiling statistics
[[email protected] bin]# Jstat-compiler 1094Compiled Failed Invalid time Failedtype failedmethod 1994 1 0 5.23 1 org/apache/tomcat/util/introspectionutils setProperty ===================compiled: Number of compilations. Failed: Number of failures Invalid: quantity not available time: Failedtype: Failure Type Failedmethod: Failed method ===================
3 Garbage Collection Statistics
[[email protected] bin]# jstat -gc 1094 s0c s1c S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 832.0 832.0 96.4 0.0 7040.0 6278.2 17372.0 16028.9 20224.0 19493.8 2304.0 2056.7 18 0.141 1 0.030 &Total capacity of the Nbsp; 0.171====================s0c : survivor0 area S1c : survivor1 area Total capacity s0u : survivor0 area used capacity S1c : survivor1 area used capacity Ec : eden area Total capacity Eu : eden area used capacity OC Total capacity of : old area Ou : old area used capacity pc current perm capacity (KB) pu perm use (KB) ygc : New Generation garbage collection times ygct : generation garbage collection time fgc : old age garbage collection times fgct : old age garbage collection time gct : Total garbage collection consumption time ====================jstat -gc 1094 3000 10 This command means that every 3000MS output 1094 GC case, output 10 times [[ email protected] bin]# jstat -gcutil 1094 used space as a percentage of total space s0 s1 e o M CCS YGC ygct fgc fgct gct 0.00 12.48 6.74 92.27 96.39 89.27 19 0.158 1 0.030 0.187 [[email protected] bin]# jstat -gccapacity 1094 with-GC, but also outputs the maximum, minimum space (heap memory statistics) used by each area of the Java heap NGCMN ngcmx ngc s0c s1c ec ogcmn ogcmx OGC OC MCMN MCMX mc ccsmn ccsmx ccsc YGC fgc 5440.0 83264.0 8704.0 832.0 832.0 7040.0 10944.0 166592.0 17372.0 17372.0 0.0 1067008.0 20224.0 0.0 1048576.0 2304.0 19 1
More jstat command parameters can be found in https://www.cnblogs.com/lizhonghua34/p/7307139.html
Three Jstack commands
Jstack is primarily used to view thread stack information within a Java process, Jstack can navigate to the thread stack, and we can target specific code based on stack information, so it is used very much in JVM performance tuning.
Command line parameter options
-L long listings, will print out additional lock information, in the event of a deadlock can use the Jstack-l PID to observe the lock holding situation
-M mixed mode not only outputs the Java stack information, but also outputs C/+ + stack information (such as the native method)
Common command Jstack PID (switch to the corresponding user)
The following is a sample description;
The instance finds the most CPU-consuming Java thread in a Java process and locates the stack information , with the command PS, top, printf, Jstack, grep.
First, find out the PID of the application process
ps -ef|grep java[[email protected] bin]# jps -l1217 sun.tools.jps.jps1094 org.apache.catalina.startup.Bootstrap[[email protected] bin]# ps -ef|grep javaroot 1094 1 0 18:35 pts/0 00:00:17 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4. x86_64/bin/java -djava.util.logging.config.file=/home/apache-tomcat-8.5.31/conf/logging.properties - djava.util.logging.manager=org.apache.juli.classloaderlogmanager -djdk.tls.ephemeraldhkeysize=2048 - djava.protocol.handler.pkgs=org.apache.catalina.webresources - dorg.apache.catalina.security.securitylistener.umask=0027 -dignore.endorsed.dirs= -classpath / home/apache-tomcat-8.5.31/bin/bootstrap.jar:/home/apache-tomcat-8.5.31/bin/tomcat-juli.jar - dcatalina.base=/home/apache-tomcat-8.5.31 -dcatalina.home=/home/apache-Tomcat-8.5.31 -djava.io.tmpdir=/home/apache-tomcat-8.5.31/temp org.apache.catalina.startup.bootstrap start
The PID of the application process is found to be 1094, and then find out which thread consumes the most CPU of the process, you can use PS-LFP 1094 or top-hp 1094来 to view CPU consumption of threads in the process
[[email protected] bin]# top -hp 1094top - 19:40:51 up 1:52 , 1 user, load average: 0.00, 0.01, 0.05threads: 44 total, 0 running, 44 sleeping, 0 stopped,    0 ZOMBIE%CPU (s): 0.0 us, 0.3 sy, 0.0 ni , 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 998628 total, 84224 free, 690548 used, 223856 buff/cacheKiB Swap: 2097148 total, 2068536 free, 28612 used. 146256 avail mem pid user pr ni virt res shr s %cpu %mem time+ command 1094 root 20 0 2778656 84996 5324 s 0.0 8.5 0:00.00 java 1095 root 20 0 2778656 84996 5324 s 0.0 8.5 0:01.01 java 1096 root 20 0 2778656 84996 5324 S 0.0 8.5 0:00.54 java 1097 root 20 0 2778656 84996 5324 S 0.0 8.5 0:00.00 java 1098 root 20 0 2778656 84996 5324 s 0.0 8.5 0:00.00 java 1099 root 20 0 2778656 84996 5324 S 0.0 8.5 0:00.00 java 1100 root 20 0 2778656 84996 5324 S 0.0 8.5 0:03.06 java 1101 root 20 0 2778656 84996 5324 S 0.0 8.5 0:01.46 java 1102 root 20 0 2778656 84996 5324 S 0.0 8.5 0:00.00 java 1103 root 20 0 2778656 84996 5324 S 0.0 8.5 0:06.76 java &nBsp; 1104 root 20 0 2778656 84996 5324 s 0.0 8.5 0:00.59 java
View the CPU time consumed by thread 1103 is the largest, and use the command to view the hexadecimal value of the thread
[[email protected] bin]# printf "%x\n" 110344f query to the value 44f for the following jstack positioning information
[Email protected] bin]# jstack 1094|grep 44f "VM periodic Task Thread" os_prio=0 tid=0x00007f602c01bc80 nid=0x44f waiting On condition
This lets you see that the VM periodic Task Thread is the most time-consuming class, and then you can go through code review code.
Four Jmap
Jmap Command Parameters
Dump: Generate heap Dump snapshot Finalizerinfo: Shows the object in the F-queue queue waiting for finalizer thread to execute Finalizer method heap: Show Java heap details Histo: Displays statistics for objects in the heap Permstat: To print permanent generation STATISTICSF: Forces a dump snapshot to be generated when-dump is not responding
Often, use Jmap to dump process memory usage into files, and then use Jhat analysis to view
Jmap-dump:format=b,file=/tmp/dump.dat 1094 generating Dump.dat files
The dump file can be viewed with tools such as Mat, VISUALVM, and Jhat.
[Email protected]/]# jmap-dump:format=b,file=/tmp/dump.dat 1094Dumping heap To/tmp/dump.dat ... Heap dump file Created[[email protected]/]# jhat-port 9999/tmp/dump.dat Reading from/tmp/dump.dat ... Dump file created Sat June 20:34:39 CST 2018Snapshot read, resolving ... Resolving 274150 objects ... Chasing references, expect, dots ... ...... ..... ................................. Eliminating duplicate references ..... ..... ....... ............................... Snapshot resolved. Started HTTP Server on port 9999Server are ready.
Below you can view the browser interface
Use of the JVM Tuning tool (Jps,jstat,jstack,jmap,jhat)