The JDK itself provides many convenient JVM performance tuning monitoring tools, in addition to the integrated VISUALVM and Jconsole, there are JPS, Jstack, Jmap, Jhat, Jstat and other small tools, this blog hope to play a useful, Let's start to understand the common tools for tuning JVM performance.
In real-world enterprise Java development, sometimes we encounter these problems:
OutOfMemoryError, Low memory
Memory leaks
Thread deadlock
Lock contention (lock contention)
The Java process consumes too much CPU
......
These problems may be overlooked by many people in their daily development (for example, some people encounter the problem just to restart the server or to increase memory, but not to delve into the root cause of the problem), but to understand and solve these problems is the Java Programmer advanced requirements. This article will introduce some commonly used JVM performance tuning monitoring tools, hoping to make a start. This article refers to a lot of information on the Internet, it is difficult to enumerate, this information on the author of the Thank you! For information on JVM performance tuning, please refer to the end of this article.
A, JPS (Java Virtual machine Process Status Tool)
The JPS is primarily used to output process state information that runs in the JVM. The syntax format is as follows:
JPS [Options] [HostID]
If you do not specify HostID, the current host or server is assumed to be the default.
The command line parameter options are described below:
-Q does not output the class name, the jar name, and the parameter of the incoming main method-m output to the parameter of the main method-L output the full-name-V output of the main class or jar to the parameters of the JVM
such as the following:
[Email protected]:/# jps-m-l2458 org.artifactory.standalone.main.main/usr/local/artifactory-2.2.5/etc/ jetty.xml29920 com.sun.tools.hat.main-port 9998/tmp/dump.dat3149 org.apache.catalina.startup.Bootstrap start30972 Sun.tools.jps.jps-m-l8247 org.apache.catalina.startup.Bootstrap start25687 com.sun.tools.hat.main-port 9999 dump.dat21711 Mrf-center.jar
B, Jstack
Jstack is primarily used to view thread stack information within a Java process. The syntax format is as follows:
jstack [option] pidjstack [option] executable corejstack [option] [[Email Protected]]remote-hostname-or-ip
The command line parameter options are described below:
-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 output Java stack information, but also output C + + stack information (such as native method)
Jstack can navigate to the thread stack, and depending on the stack information we can navigate to the specific code, so it is used very much in JVM performance tuning. Let's take a look at the most CPU-consuming Java threads in a Java process and locate the stack information, with the commands PS, top, printf, Jstack, grep.
The first step is to find out the Java process ID, and the Java application name I deployed on the server is mrf-center:
[Email protected]:/# ps-ef | grep Mrf-center | Grep-v greproot 21711 1 1 14:47 pts/3 00:02:10 Java-jar Mrf-center.jar
Get the process ID 21711, the second step to find the process CPU-consuming threads, you can use PS-LFP pid or ps-mp pid-o thread, tid, time or top-hp pid, I use a third, the output is as follows:
The time column is the CPU for each Java thread, and the longest CPU time is a thread with a thread ID of 21742.
printf "%x\n" 21742
The hexadecimal value of 21742 is 54ee, which is used below.
OK, the next step is finally Jstack, which is used to output the stack information for process 21711, and then grep based on the thread ID's hexadecimal value, as follows:
[Email protected]:/# jstack 21711 | grep 54ee "Pollintervalretryschedulerthread" prio=10 tid=0x00007f950043e000 Nid=0x54ee in object.wait () [ 0X00007F94C6EDA000]
You can see the CPU consumption in the Pollintervalretryschedulerthread class of object.wait (), I looked up my code, and navigate to the following code:
Idle Waitgetlog (). info ("Thread [" + getName () + "] is idle waiting ..."); schedulerthreadstate = Polltaskschedulerthreads Tate. Idlewaiting;long now = System.currenttimemillis (), long waitTime = Now + getidlewaittime (); Long timeuntilcontinue = Waitti Me-now;synchronized (Siglock) {try {if (!halted.get ()) {siglock.wait (timeuntilcontinue); }} catch (Interruptedexception ignore) {}}
It is the idle wait code for the polling task, and the above siglock.wait (timeuntilcontinue) corresponds to the previous object.wait ().
C, Jmap (Memory Map) and Jhat (Java Heap analysis Tool)
Jmap is used to view heap memory usage, typically combined with jhat.
The JMAP syntax format is as follows:
jmap [option] pidjmap [option] executable corejmap [option] [[Email Protected]]remote-hostname-or-ip
If you are running on a 64-bit JVM, you may need to specify the-J-D64 command option parameter.
Jmap-permstat PID
The print process's class loader and ClassLoader load the persisted object information, Output: Class loader name, whether the object is alive (unreliable), object address, parent ClassLoader, loaded class size, and so on, such as:
Use the Jmap-heap PID to view the process heap memory usage, including the GC algorithm used, heap configuration parameters, and heap memory usage in each generation. For example, the following:
[Email protected]:/# jmap -heap 21711attaching to process id 21711, please wait ... Debugger attached successfully. Server compiler detected. Jvm version is 20.10-b01using thread-local object allocation. Parallel gc with 4 thread (s) heap configuration: minheapfreeratio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 2067791872 (1972.0MB) NewSize = 1310720 (1.25MB) MaxNewSize = 17592186044415 MB OldSize = 5439488 (5.1875MB) NewRatio = 2 survivorratio = 8 permsize = 21757952 (20.75MB) MaxPermSize = 85983232 (82.0MB) heap usage:ps young generationeden space: capacity = 6422528 (6.125MB) used = 5445552 (5.1932830810546875MB) free = 976976 ( 0.9317169189453125MB) 84.78829520089286% usedfrom space: capacity = 131072 (0.125MB) used = 98304 ( 0.09375MB) free = 32768 (0.03125MB) 75.0% usedto space: capacity = 131072 (0.125MB) used = 0 (0.0MB) free = 131072 (0.125MB) 0.0% usedPS Old Generation capacity = 35258368 ( 33.625MB) used = 4119544 (3.9287033081054688MB) free = 31138824 (29.69629669189453MB) 11.683876009235595% usedps perm generation capacity = 52428800 (50.0MB) used = 26075168 (24.867218017578125MB) free = 26353632 (25.132781982421875MB) 49.73443603515625% used
Use jmap-histo[:live] PID to view the number of objects in heap memory, size statistics histogram, if you bring live, only live objects, as follows:
[email protected]:/# jmap -histo:live 21711 | more num #instances #bytes class Name---------------------------------------------- 1: 38445 5597736 <constmethodklass > 2: 38445 5237288 <methodKlass> 3: 3500 3749504 <constantPoolKlass> 4: 60858 3242600 <symbolKlass> 5: 3500 2715264 < instanceklassklass> 6: 2796 2131424 <constantPoolCacheKlass> 7: 5543 1317400 [I 8: 13714 1010768 [c 9: 4752 1003344 [B 10: 1225 639656 <methodDataKlass> 11: 14194 454208 java.lang.string 12: 3809 396136 java.lang.Class 13: 4979 311952 [S 14: 5598 287064 [[i 15: 3028 266464 java.lang.reflect.method 16 : 280 163520 <objArrayKlassKlass> 17: 4355 139360 java.util.hashmap$entry 18: 1869 138568 [Ljava.util.HashMap$Entry; 19: 2443 97720 java.util.linkedhashmap$entry 20: 2072 82880 java.lang.ref.SoftReference 21: 1807 71528 [Ljava.lang.Object; 22: 2206 70592 java.lang.ref.weakreference 23: 934 52304 java.util.LinkedHashMap 24: 871 48776 java.beans.MethodDescriptor 25: 1442 46144 java.util.concurrent.concurrenthashmap$hashentry 26: 804 38592 java.util.hashmap 27: 948 37920 java.util.concurrent.concurrenthashmap$ segment 28: 1621 35696 [ljava.lang.class; 29: 1313 34880 [Ljava.lang.String; 30: 1396 33504 java.util.LinkedList$Entry 31: 462 33264 java.lang.reflect.field 32: 1024 32768 java.util.hashtable$entry 33 : 948 31440 [ljava.util.concurrent.concurrenthashmap$hashentry;
Class name is the object type, as described below:
B Bytec Chard Doublef floati intj longz boolean[array, such as [i = int[][l+ class name other objects
Another common scenario is using JMAP to dump process memory usage into a file, and then use the Jhat analysis to view it. Jmap the dump command format as follows:
Jmap-dump:format=b,file=dumpfilename
I dump the above process ID 21711 as well:
[Email protected]:/# jmap-dump:format=b,file=/tmp/dump.dat 21711 dumping heap to/tmp/dump.dat ... Heap dump file created
The dump file can be viewed with tools such as Mat, VISUALVM, and Jhat:
[Email protected]:/# jhat-port 9998/tmp/dump.datreading From/tmp/dump.dat ... Dump file created Tue Jan 17:46:14 CST 2014Snapshot Read, resolving ... Resolving 132207 objects ... Chasing references, expect, dots ................. Eliminating duplicate references ......... .......... Snapshot resolved. Started HTTP Server on port 9998Server are ready.
Then you can enter the host address in the browser: 9998 viewed:
Above the red box out of the section you can find their own, the last one Support OQL (object query Language).
D, Jstat (JVM Statistical monitoring tool)
The syntax format is as follows:
Jstat [generaloption | outputoptions vmid [INTERVAL[S|MS] [count]]
Vmid is the virtual machine ID, which is generally the process ID on the Linux/unix system. Interval is the sampling time interval. Count is the number of samples. For example, the following output is GC information, sampling time interval is 250ms, sample number is 4:
[email protected]:/# jstat -gc 21711 250 4 s0c s1c S0U S1U EC EU OC OU PC PU ygc ygct fgc fgct GCT 192.0 192.0 64.0 0.0 6144.0 1854.9 32000.0 4111.6 55296.0 25472.7 702 0.431 3 0.218 0.649192.0 192.0 64.0 0.0 6144.0 1972.2 32000.0 4111.6 55296.0 25472.7 702 0.431 3 0.218 0.649192.0 192.0 64.0 0.0 6144.0 1972.2 32000.0 4111.6 55296.0 25472.7 702 0.431 3 0.218 0.649192.0 192.0 64.0 0.0 6144.0 2109.7 32000.0 4111.6 55296.0 25472.7 702 0.431 3 0.218 0.649
To understand the meaning of the above columns, first look at the JVM heap memory layout:
Can be seen:
Heap memory = young generation + old generation + permanent generation young generation = Eden Zone + two survivor zones (from and to)
Now to explain the meaning of each column:
s0c, s1c, s0u, S1u:survivor 0/1-zone capacity (capacity) and usage (used) EC, Eu:eden area capacity and usage oc, OU: Old generation capacity and Usage pc, PU: Permanent generation capacity and usage ygc, YGT: Young generation GC times and GC time-consuming FGC, fgct:full GC times, and full GC time-consuming GCT:GC total time-consuming
Other JVM performance tuning references:
Java Virtual Machine Specification
"Java Performance"
"Trouble Shooting Guide for Javase 6 with HotSpot VMs": http://www.oracle.com/technetwork/java/javase/tsg-vm-149989.pdf
"Effective Java"
visualvm:http://docs.oracle.com/javase/7/docs/technotes/guides/visualvm/
Jconsole:http://docs.oracle.com/javase/1.5.0/docs/guide/management/jconsole.html
Monitoring and managing Javase 6 applications:http://www.oracle.com/technetwork/articles/javase/monitoring-141801.html
JVM Performance Tuning monitoring Tools JPS, Jstack, Jmap, Jhat, Jstat use detailed