Personal blog project address

Hope everybody helps point a star, add a small star to me ✨


This document documents common JVM directives, which can be viewed using Java’s powerful bin directory.

Many of these commands refer to the option parameter, which you have to type several times to remember.


JVM commands

The parameter in square brackets [] indicates that it is optional.


jps


JVM Process Status Tool, which displays all HotSpot VIRTUAL machine processes in a specified system.

The command format

jps [option] [hostid]
Copy the code

The option parameter

-l-q: Outputs only LVMID -m: outputs the parameters passed to main() when VMS are started. -v: Outputs the specified JVM parameters when VMS are startedCopy the code

Example:

[root@VM_247_254_centos ~]#jps -lm 26176 org.apache.zookeeper.server.quorum.QuorumPeerMain / usr/local/zookeeper - 3.4.10 / bin /.. CFG 25044 /usr/local/apache-activemq-5.14.5//bin/activemq.jar start 23732 sun.tools.jps.jps-lm 25446 org.apache.catalina.startup.Bootstrap startCopy the code

The first digit represents PID, which is useful later.


jstat


Jstat (JVM Statistics Monitoring) is a command used to monitor the status of a virtual machine while it is running. It shows the running data of a virtual machine, such as class loading, memory, garbage collection, JIT compilation, and so on.

The command format

Jstat [option] LVMID [interval] [count] [option] : Operation parameters LVMID: ID of the local VM process [interval] : interval for continuous output [count] : number of consecutive output timesCopy the code

The option parameter

Option explain
class Class Loader behavior statistics. Statistics on the behavior of the class loader.
compiler HotSpt JIT compiler behavior statistics. Statistics of the behavior of the HotSpot Just-in-Time compiler.
gc Garbage collection heap behavior statistics. Statistics of the behavior of the garbage collected heap.
gccapacity The capacity of each garbage collection generation (young,old, PERM) and their corresponding spatial statistics. Statistics of the capacities of the generations and their corresponding spaces.
gcutil Overview of garbage collection statistics. Summary of garbage collection statistics.
gccause Overview of garbage collection statistics (same as -gcutil), plus the cause of the last two garbage collection events. Summary of garbage collection statistics (same as -gcutil), with the cause of the last and
gcnew Cenozoic behavior statistics. Statistics of the behavior of the new generation.
gcnewcapacity Statistics of the new generation and its corresponding memory space. Statistics of the sizes of the new generations and its corresponding spaces.
gcold Behavior statistics of the aged and immortal generations. Statistics of the behavior of the old and permanent generations.
gcoldcapacity Behavior statistics of the aged generation. Statistics of the sizes of the old generation.
gcpermcapacity Behavior statistics of immortal generation. Statistics of the sizes of the permanent generation.
printcompilation HotSpot compile method statistics. HotSpot compilation method statistics.

For example, look at the behavior statistics of the garbage collection heap

[root@VM_247_254_centos ~]# jstat -gc 25446
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
6400.0 6400.0  0.0   1601.4 51712.0  50837.0   128808.0   88450.0   67584.0 66167.7 7936.0 7630.5    401    5.939  10      1.247    7.186
Copy the code

C: Capacity indicates the Capacity. U: Used indicates the Used Capacity

S0C: total capacity of survivor0 S1C: Total capacity of survivor1 S0U: used capacity of survivor0 S1C: Used capacity of Survivor1 EC: Total capacity of Eden EU: Used capacity in Eden area OC: Total capacity in the Old area OU: used capacity in the Old area PC Current perM capacity (KB) PU PERM usage (KB) YGC: number of garbage collection times in the new generation YGCT: garbage collection time in the new generation FGC: Garbage collection times in the old age FGCT: garbage collection time in the old age GCT: total garbage collection timeCopy the code

There are a number of options. You can type them one by one to see what works.


jmap


Jmap (JVM Memory Map) command is used to generate heap dump file, if you don’t use this command, you can use – XX: + HeapDumpOnOutOfMemoryError parameters to make the virtual machine appears OOM, automatically generate the dump file. Jmap can not only generate dump files, but also query finalize execution queue, Java heap, and persistent generation details, such as current usage, which collector is currently in use, etc.

The command format

jmap [option] LVMID
Copy the code

The option parameter

Dump: snapshot of raw heap dump finalizerinfo: displays objects in the F-queue that are waiting for the Finalizer thread to execute the Finalizer method. Heap: displays detailed Information about the Java heap. Histo: Display statistics of objects in the heap permstat: toprintPermanent generation statistics F: If -dump does not respond, a dump snapshot is forcibly generatedCopy the code

An 🌰

  • -dump
jmap -dump:format=b,file=dump.dprof 25446
Dumping heap to /home/gem/dump.dprof ...
Heap dump file created
Copy the code

After the. Dprof file is output, use MAT analysis tool for analysis

  • -heap Displays the summary information about the heap, the algorithm used by GC, the heap configuration, and the wise heap usage. You can use this information to determine the current memory usage and garbage collection status
jmap -heap 25446 Attaching to process ID 25446, please wait... Debugger Attached successfully. Server Compiler detected. JVM version is 25.121-B13 using ththread -local object // Heap Configuration: // Corresponds to the JVM startup parameter -xx :MinHeapFreeRatio Sets the minimum heap idle ratio of the JVM (default 40 M)inHeapFreeRatio = 40 // The corresponding JVM startup parameter -xx :MaxHeapFreeRatio Sets the maximum heap idle ratio of the JVM (default 70) MaxHeapSize= 262144000 (250.0MB) MaxHeapSize= 262144000 (250.0MB) // The JVM startup parameter -xx :NewSize= the default size of the 'new generation' JVM heap NewSize= 5570560 (5.3125MB) // Corresponding JVM startup parameter -xx :MaxNewSize= Set the maximum size of the 'new generation' (YG) of the JVM heap MaxNewSize= 87359488 (83.3125MB) -xx :OldSize=<value>: Set the size of the heap OldSize= 11206656 (10.6875MB) // The corresponding JVM startup parameter -xx :NewRatio=: size ratio between 'new generation' and 'old age' NewRatio= 2 // The corresponding JVM startup parameter -xx :SurvivorRatio= Sets the size ratio between Eden zone and Survivor zone in the young generation SurvivorRatio = 8 // MetaspaceSize, corresponding to -xx :MetaspaceSize, the initial space size // will trigger garbage collection for type offload. At the same time the GC can adjust the value / / JDK 8 permanent generation to dimension MetaspaceSize = 21807104 (20.796875 MB) / / only when - XX: + UseCompressedClassPointers opened up / / by Java - XX: + PrintFlagsInitial | grep UseCompressedClassPointers / / found bool UseCompressedClassPointers =false. Yes CompressedClassSpaceSize = 1073741824 (1024.0MB) // Corresponding startup parameter -xx :MaxMetaspaceSize MaxMetaspaceSize = MaxMetaspaceSize = 17592186044415 MB // When using G1 collector, set the size of the Java heap to be split. This size ranges from 1M to 32M. G1HeapRegionSize = 0 (0.0MB) // Heap Usage: New Generation (Eden + 1 Survivor Space): Capacity = 59506688 (56.75MB) Used = 17941224 (17.110084533691406MB) Free = 41565464 (39.639915466308594MB) 30.14992869372935% Used Capacity = 52953088 (50.5MB) Used = 17935840 (17.104949951171875MB) Free = 35017248 (33.395050048828125MB) 33.871188022122524% used // Memory distribution in one of the Survivor zones From Space: Capacity = 6553600 (6.25MB) Used = 5384 (0.00513458251953125MB) Free = 6548216 (6.244865417480469MB) 0.0821533203125% Used // Other Survivor area memory distribution To Space: Capacity = 6553600 (6.25MB) Used = 0 (0.0mb) Free = 6553600 (6.25MB) 0.0% Used Capacity = 131899392 (125.7890625MB) Used = 94610832 (90.22792053222656MB) Free = 37288560 (35.56114196777344MB) 71.7295436812931% Used 27405 Interned Strings evicted 3101144 bytes.Copy the code

jhat

The JHAT (JVM Heap Analysis Tool) command is used with Jmap to analyze dump results generated by Jmap. Jhat has a built-in miniature HTTP/HTML server. After dump Analysis results are generated, you can view them in a browser. Note that the dump file generated by the server is usually copied to local or other machines for analysis because jHAT is a time-consuming and resource-consuming process.

Because the common analysis method is to use MAT common to all platforms for analysis, it is not specific to display the effect in remote server operation.


jstack

Jstack is used to generate a thread snapshot of the Java VIRTUAL machine at the current time. A thread snapshot is a collection of method stacks that are being executed by each thread in the Java VIRTUAL machine (JVM). The main purpose of a thread snapshot is to locate the cause of a long pause in a thread, such as deadlocks, loops, and long waits caused by requests for external resources. By looking at the call stack of each thread when it pauses, you can see what the unresponsive thread is doing in the background, or what resources it is waiting for. If a Java program crashes to generate a core file, the JStack tool can be used to get information about the Java Stack and native stack of the core file, so you can easily know how the Java program crashed and where the problem occurred in the program. In addition, the JStack tool can attach to a running Java program and see information about the Java Stack and native Stack of the running Java program. Jstack is very useful if the current running Java program appears hung.

The command format

jstack [option] LVMID
Copy the code

The option parameter

-f: Forces the thread stack to be output when a normal output request is not responded to-l: Displays additional information about the lock in addition to the stack. -m: Displays the C/C++ stack if a local method is calledCopy the code

For example

jstack -l25446 | more the 2018-01-25 21:18:22 Full thread dump the Java HotSpot (TM) 64 - Bit Server VM (25.121 - bl3 mixed mode) :"main-EventThread" #174 daemon prio=5 os_prio=0 tid=0x00007feb692b7000 nid=0x6502 waiting on condition [0x00007feb32bb1000]State: WAITING (parking) at sun.misc.Unsafe. Park (Native Method) - parking towait for  <0x00000000f0a00860> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:501)

   Locked ownable synchronizers:
	- None

"The main - SendThread (115.159.192.69:2181)" #173 daemon prio=5 os_prio=0 tid=0x00007feb694bd800 nid=0x6501 runnable [0x00007feb363ce000]/ / run Java. Lang. Thread. The State: RUNNABLE at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method) at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269) at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93) at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86) - locked <0x00000000f09ef268> (a sun.nio.ch.Util$3)
	- locked <0x00000000f09ef258> (a java.util.Collections$UnmodifiableSet) - locked <0x00000000f09ef140> (a sun.nio.ch.EPollSelectorImpl) at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)  at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:349) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1141)

   Locked ownable synchronizers:
	- None
Copy the code

jinfo

The jinfo(JVM Configuration info) command is used to view and adjust vm running parameters in real time. The previous JPS -v password can only be viewed for parameters specified in the display. If you want to view the values of parameters specified in the display, you need to use the jinfo password

I usually use it to look at JVM startup parameters

The command format

jinfo [option] [args] LVMID
Copy the code

Optin parameters

-flags: output the values of the specified ARgs parameter. -flags: output the values of all JVM parameters without the args parameter. -sysprops: Output System properties, equivalent to system.getProperties ()Copy the code

An 🌰

[root@VM_247_254_centos ~]# jinfo -flags 25446
Attaching to process ID 25446, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.121-b13
Non-default VM flags: -XX:CICompilerCount=2 -XX:InitialHeapSize=16777216 -XX:MaxHeapSize=262144000 -XX:MaxNewSize=87359488 -XX:MinHeapDeltaBytes=196608 -XX:NewSize=5570560 -XX:OldSize=11206656 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops
Command line:  -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp
Copy the code

As can be seen from the above, the parameters printed out by JINFO are intended to be written in the next article about the use of MAT, but I have manually made exceptions, and the exception information is concentrated in Java Objects. As long as I associate with the code changed last time, I can find the possible places of large Objects. Therefore, for specific analysis, After getting a more complex dump file to learn in detail.


The resources

JPS jstat jmap jhat jstack jinfo

2. Java8 memory model — PermGen and Metaspace

3. Understanding the JVM by Zhiming Zhou