JVM Monitoring Tools

Source: Internet
Author: User
Tags visualvm artifactory

In addition to the integrated VISUALVM and Jconsole, the JVM also has small tools such as JPS, Jstack, Jmap, Jhat, Jstat, etc.

Problems that may be encountered in development

    • 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 introduces some common JVM performance tuning monitoring tools.

1. 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

2.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 ().

3.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 = Maxheapfreeratio = Maxheapsize = 20677  91872 (1972.0MB) NewSize = 1310720 (1.25MB) maxnewsize = 17592186044415 MB oldsize = 5439488 (5.1875MB) Newratio = 2 Survivorratio = 8 PermSize = 21757952 (20.75MB) MaxPermSize = 859 83232 (82.0MB) Heap usage:ps young Generationeden space:capacity = 6422528 (6.125MB) used = 5445552 (5.19328308105 46875MB) free = 976976 (0.9317169189453125MB) 84.78829520089286% usedfrom space:capacity = 131072 (0.125MB) u     sed = 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.696296691894 53MB) 11.683876009235595% Usedps Perm Generation capacity = 52428800 (50.0MB) used = 26075168 (24.86721801757812 5MB) 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:38,445 5597736 <constMethodKlass> 2:38445 5237288 <methodKlass> 3:3,500 374950 4 <constantPoolKlass> 4:60858 3242600 <symbolKlass> 5:3500 2715264 &lt ;instanceklassklass> 6:2796 2131424 <constantPoolCacheKlass> 7:5,543 131740 0 [I 8:13714 1010768 [C 9:4752 1003344 [B 10:1225 639656 <m  ethoddataklass> 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:4,355         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.SoftRefer  ence 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.Conc Urrenthashmap$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 3 2: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).

4.jstat (JVM Statistics 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 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

  

JVM Monitoring Tools

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.