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

How to use the JVM performance tuning monitoring tool

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the JVM performance tuning monitoring tool". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the JVM performance tuning monitoring tool.

In the development and maintenance of enterprise-level Java applications, 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 and maintenance (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 a necessary requirement for Java programmers. This article will introduce some commonly used JVM performance tuning monitoring tools, hoping to serve as a valuable reference.

And the use of these monitoring and tuning tools, whether you are operation and maintenance, development, testing, must be mastered.

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 into the main method

-m outputs the parameters passed into 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 grep

Root 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-b01

Using 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 Generation

Eden Space:

Capacity = 6422528 (6.125MB)

Used = 5445552 (5.1932830810546875MB)

Free = 976976 (0.9317169189453125MB)

84.78829520089286% used

From Space:

Capacity = 131072 (0.125MB)

Used = 98304 (0.09375MB)

Free = 32768 (0.03125MB)

75.0% used

To Space:

Capacity = 131072 (0.125MB)

Used = 0 (0.0MB)

Free = 131072 (0.125MB)

0.0% used

PS Old Generation

Capacity = 35258368 (33.625MB)

Used = 4119544 (3.9287033081054688MB)

Free = 31138824 (29.69629669189453MB)

11.683876009235595% used

PS 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

4: 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, such as [I for 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.dat

Reading from / tmp/dump.dat...

Dump file created Tue Jan 28 17:46:14 CST 2014Snapshot read, resolving...

Resolving 132207 objects …

Chasing references, expect 26 dots.

Eliminating duplicate references...

Snapshot resolved.

Started HTTP server on port 9998Server 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 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.649

192.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.649

192.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.649

192.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:

As can be seen:

Heap memory = younger generation + older generation + permanent generation

Young generation = Eden region + two Survivor regions (From and To)

Now let's explain the meaning of each column:

S0C, S1C, S0U, S1U:Survivor 0Universe Zone 1 capacity (Capacity) and usage (Used)

Capacity and usage of EC and EU:Eden zones

OC, OU: annual capacity and usage of older generation

PC, PU: permanent generation capacity and usage

YGC, YGT: the number of GC and GC time of the younger generation

FGC, number of FGCT:Full GC and Full GC time

Total GCT:GC time-consuming

E, hprof (Heap/CPU Profiling Tool)

Hprof can show CPU usage and count heap memory usage.

The syntax format is as follows:

Java-agentlib:hprof [= options] ToBeProfiledClass

Java-Xrunprof [: options] ToBeProfiledClass

Javac-J-agentlib:hprof [= options] ToBeProfiledClass

The complete command options are as follows:

Option Name and Value Description Default

Heap=dump | sites | all heap profiling all

Cpu=samples | times | old CPU usage off

Monitor=y | n monitor contention n

Format=a | b text (txt) or binary output a

File= write data to file java.hprof [.txt]

Net=: send data over a socket off

Depth= stack trace depth 4

Interval= sample interval in ms 10

Cutoff= output cutoff point 0.0001

Lineno=y | n line number in traces? Y

Thread=y | n thread in traces? N

Doe=y | n dump on exit? Y

Msa=y | n Solaris micro state accounting n

Force=y | n force output to y

Verbose=y | n print messages about dumps y

Here are some examples from the official guide.

Example of CPU Usage Sampling Profiling (cpu=samples):

Java-agentlib:hprof=cpu=samples,interval=20,depth=3 Hello

The above samples CPU consumption information every 20 milliseconds, the stack depth is 3, and the generated profile file name is java.hprof.txt, in the current directory.

The example of CPU Usage Times Profiling (cpu=times), which can obtain more fine-grained CPU consumption information than CPU Usage Sampling Profile, can be as fine-grained as the beginning and end of each method call, and its implementation uses bytecode injection (BCI):

Javac-J-agentlib:hprof=cpu=times Hello.java

Example of Heap Allocation Profiling (heap=sites):

Javac-J-agentlib:hprof=heap=sites Hello.java

An example of Heap Dump (heap=dump), which generates more detailed Heap Dump information than the Heap Allocation Profiling above:

Javac-J-agentlib:hprof=heap=dump Hello.java

Although adding the-Xrunprof:heap=sites parameter to the JVM startup parameters can generate CPU/Heap Profile files, it has a great impact on JVM performance and is not recommended for online server environments.

Thank you for reading, the above is the content of "how to use the JVM performance tuning monitoring tool". After the study of this article, I believe you have a deeper understanding of how to use the JVM performance tuning monitoring tool. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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