Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the monitoring tools for JVM performance tuning and their use?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what the JVM performance tuning monitoring tools are and how to use them. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

In real enterprise Java development, we sometimes encounter the following problems:

OutOfMemoryError, out of memory

Memory leak

Thread deadlock

Lock contention (Lock Contention)

Java process consumes too much CPU

.

These problems may be ignored by many people in daily development (for example, some people encounter the above problems just to restart the server or increase memory, rather than delving into the root cause of the problem), but being able to understand and solve these problems is an advanced requirement for Java programmers. This paper introduces some commonly used JVM performance tuning monitoring tools, hoping to serve as a valuable reference.

A, jps (Java Virtual Machine Process Status Tool)

Jps is mainly used to output process status information running in JVM. The syntax format is as follows:

Jps [options] [hostid]

If you do not specify hostid, it defaults to the current host or server.

The command line parameter options are described as follows:

-Q does not output the class name, Jar name and parameters passed in the main method-m outputs the parameters passed in the main method-l outputs the fully qualified name of the main class or Jar-v outputs the parameters passed into JVM

Such as the following:

Root@ubuntu:/# jps-m-l 2458 org.artifactory.standalone.main.Main / usr/local/artifactory-2.2.5/etc/jetty.xml 29920 com.sun.tools.hat.Main-port 9998 / tmp/dump.dat 3149 org.apache.catalina.startup.Bootstrap start 30972 sun.tools.jps.Jps-m-l 8247 org.apache.catalina.startup.Bootstrap start 25687 com.sun.tools.hat.Main-port 9999 dump.dat 21711 mrf-center.jar

B 、 jstack

Jstack is mainly used to view thread stack information within a Java process. The syntax format is as follows:

Jstack [option] pid jstack [option] executable core jstack [option] [server-id@] remote-hostname-or-ip

The command line parameter options are described as follows:

-l long listings, which prints out additional lock information, and you can use jstack-l pid to observe lock holdings when deadlocks occur-m mixed mode, which outputs not only Java stack information, but also Java + stack information (such as the Native method).

Jstack can navigate to the thread stack, and we can navigate to specific code based on stack information, so it is used a lot in JVM performance tuning. Let's take an example to find the Java thread that consumes the most CPU in a Java process and locate the stack information. The commands used are ps, top, printf, jstack, and grep.

The first step is to find out the Java process ID. The name of the Java application I deployed on the server is mrf-center:

Root@ubuntu:/# ps-ef | grep mrf-center | grep-v greproot 21711 11 14:47 pts/3 00:02:10 java-jar mrf-center.jar

The ID of the process is 21711. The second step is to find out the thread that consumes the most CPU in the process. You can use ps-Lfp pid or ps-mp pid-o THREAD, tid, time or top-Hp pid. I use the third one here, and the output is as follows:

The TIME column is the CPU time consumed by each Java thread. The longest CPU time is the thread with an ID of 21742.

Printf "% x\ n" 21742

The hexadecimal value of 21742 is 54ee, which will be used below.

OK, the next step is finally jstack, which is used to output stack information of process 21711, and then according to the hexadecimal value grep of thread ID, as follows:

Root@ubuntu:/# jstack 21711 | grep 54ee "PollIntervalRetrySchedulerThread" prio=10 tid=0x00007f950043e000 nid=0x54ee in Object.wait () [0x00007f94c6eda000]

You can see that CPU consumes Object.wait () in the class PollIntervalRetrySchedulerThread. I look for my code and navigate to the following code:

/ / Idle wait getLog () .info ("Thread [" + getName () + "] is idle waiting..."); schedulerThreadState = PollTaskSchedulerThreadState.IdleWaiting; long now = System.currentTimeMillis (); long waitTime = now + getIdleWaitTime (); long timeUntilContinue = waitTime-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 sigLock.wait (timeUntilContinue) above corresponds to the previous Object.wait ().

C, jmap (Memory Map) and jhat (Java Heap Analysis Tool)

Jmap is used to view heap memory usage, usually in conjunction with jhat.

The format of the jmap syntax is as follows:

Jmap [option] pid jmap [option] executable core jmap [option] [server-id@] remote-hostname-or-ip

If you are running on a 64-bit JVM, you may need to specify the-J-d64 command option argument.

Jmap-permstat pid

Print the persistent object information loaded by the class loader and class loader of the process, and output: class loader name, object survival (unreliable), object address, parent class loader, loaded class size and other information, as shown below:

Use jmap-heap pid to view process heap memory usage, including the GC algorithm used, heap configuration parameters, and heap memory usage in each generation. For example, the following example:

Root@ubuntu:/# jmap-heap 21711 Attaching 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 statistical histogram of the number and size of objects in heap memory. With live, only live objects are counted, as follows:

Root@ubuntu:/# jmap-histo:live 21711 | more num # instances # bytes class name- 1: 38445 5597736 2: 38445 5237288 3: 3500 3749504: 60858 3242600 5: 3500 2715264 6: 2796 2131424 7: 5543 1317400 [I 8: 13714 1010768 [C 9: 4752 1003344 [B 10: 1225 639656 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 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 an object type, which is described as follows:

B byte C char D double F float I int J long Z boolean [array, for example, [I represents int [] [L+ class name] other objects

There is also a very common situation: use jmap to dump the process memory usage into a file, and then use jhat analysis to see. The format of the dump command for jmap is as follows:

Jmap-dump:format=b,file=dumpFileName pid

I also Dump the above process with an ID of 21711:

Root@ubuntu:/# jmap-dump:format=b,file=/tmp/dump.dat 21711 Dumping heap to / tmp/dump.dat... Heap dump file created

The files from dump can be viewed with MAT, VisualVM and other tools. Here, you can view them with jhat:

Root@ubuntu:/# jhat-port 9998 / tmp/dump.datReading from / tmp/dump.dat...Dump file created Tue Jan 28 17:46:14 CST 2014 Snapshot read, resolving...Resolving 132207 objects...Chasing references, expect 26 dots..Eliminating duplicate references..Snapshot resolved.Started HTTP server on port 9998 Server is ready.

Note that if the Dump file is too large, you may need to add the parameter-J-Xmx512m to specify the maximum heap memory, that is, jhat-J-Xmx512m-port 9998 / tmp/dump.dat. Then you can enter the host address: 9998 in the browser to view:

You can explore the red box above. The last item supports OQL (object query language).

D, jstat (JVM Statistical Monitoring tool)

The syntax format is as follows:

Jstat [generalOption | outputOptions vmid [intervals | ms] [count]]

Vmid is the Java virtual machine ID, which is generally the process ID on Linux/Unix systems. Interval is the sampling interval. Count is the number of samples. For example, the following output is the GC information. The sampling interval is 250ms and the number of samples is 4:

Root@ubuntu:/# jstat-gc 21711 2504 S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT 192.0 192.0 64.00.0 6144.0 1854.9 32000.0 4111.6 55296.0 25472.7 details about JVM performance tuning monitoring tools and their use are shared here I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report