This article mainly introduces the JDK built-in commands, without more words, let’s get started!

javap

You can use javap to view Java bytecode decompiled source files. The javap command format is as follows:

HelloWorld class HelloWorld class HelloWorld class HelloWorld

public class HelloWorld {
    public static void main(String []args) {
       System.out.println("Hello World"); }}Copy the code

Using javap -c helloworld.class, you can decompile as follows:

Compiled from "HelloWorld.java"
public class HelloWorld {
  public HelloWorld();
    Code:
       0: aload_0
       1: invokespecial #1 // Method java/lang/Object."
      
       ":()V
      
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2 // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3 // String Hello World
       5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;) V
       8: return
}
Copy the code

jps

JPS is used to query the PID of all processes. The usage of JPS is as follows:

The pid of the native Java program can be obtained by executing JPS. The result is as follows:

[root@wupx ~]# jps8825 spring - the boot - 0.0.1 - the SNAPSHOT. The jarCopy the code

Using JPs-MLVv, you can get the PROCESS PID, jar package name, JVM parameters, and so on.

[root@wupx ~]# jps -mlvV8825 / root/spring - the boot - 0.0.1 - the SNAPSHOT. The jar - server port = 8090 -- logging. The file = / root /log/spring-boot.log -Xmx1024m -Xms1024m
Copy the code

jstat

Jstat is used to monitor THE JVM, mainly for GC information, and is often used for performance optimization. The command content is as follows:

Jstat -gc 8825: jstat -gc 8825: jstat -gc 8825

[root@wupx ~]# jstat -gc 8825
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
65536.0 69120.0  0.0   160.0  10425344.0 1036247.8 21135360.0 19489859.7 84608.0 81123.8 9600.0 8834.1  99517 2070.459   0      0.000 2070.459
Copy the code

S0C indicates the capacity of current Survivor0, S1C indicates the capacity of current Survivor1, S0U indicates the utilization of current Survivor0, S1U indicates the utilization of current Survivor1, EC indicates the capacity of Eden, EU represents the utilization of Eden, OC represents the capacity of the old age, OU represents the utilization of the old age, MC represents the capacity of Metaspace, MU represents the utilization of Metaspace, CCSC represents the capacity of the space compressed by the class pointer, CCSU represents the space compressed by the class pointer, YGC represents the number of Cenozoic GC, YGCT represents the time of Cenozoic GC, FGC represents the number of Full GC, FGCT represents the time of Full GC, and GCT represents the total GC time.

Each object has a pointer to its own class, _klass: a 4-byte pointer to a class, and _klass: an 8-byte pointer to a class on 64-bit platforms. To save these Spaces, class pointer compression was introduced.

jcmd

To view JVM information, run the following commands:

Use JCMD 8825 help to see what commands are supported:

[root@wupx ~]# jcmd 8825 help
8825:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
VM.classloader_stats
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.finalizer_info
GC.heap_info
GC.run_finalization
GC.run
VM.uptime
VM.dynlibs
VM.flags
VM.system_properties
VM.command_line
VM.version
help
Copy the code

Heap_dump JCMD 8825 gc. heap_dump JCMD 8825 gc. heap_dump JCMD 8825 gC. heap_dump

[root@wupx ~]# jcmd 8825 GC.heap_info8825: PSYoungGen total 628736K, used 41772K [0x0000000715a00000, 0x0000000746480000, 0x00000007c0000000) eden space 609792K, 4%, informs [x0000000715a00000 0, 0 x00000007173d5478, 0 x000000073ad80000) from space 18944 k, 80%, informs [x000000073c000000 x000000073ad80000 0, 0 x000000073bc75e68, 0) to space 19968 k, 0%, informs [x0000000745100000 0, 0 x0000000745100000, 0 x0000000746480000) ParOldGen total 250880 k, used 21756K [0x00000005c0e00000, 0x00000005d0300000, 0x0000000715a00000) object space 250880K, 8%, informs [x00000005c0e00000 0, 0 x00000005c233f160, 0 x00000005d0300000) Metaspace informs the 44797 k, capacity 45562 k, committed 45824K, reserved 1089536K class space used 5669K, capacity 5832K, committed 5888K, reserved 1048576KCopy the code

You can see that you can get the size and proportion of the new generation, old age, meta-space, Eden, From Survivor, and To Survivor.

jmap

Jmap prints out objects in Java process memory, or the heap in the JVM as binary text, using the following command:

Use jmap-heap 8825 to view the current heap usage:

[root@wupx ~]# jmap -heap 8825Attaching to process ID 8825, please wait... Debugger attached successfully. Server Compiler detected. JVM version is 25.201-B09 using ththread -local object allocation. Parallel GC with 10 thread(s) Heap Configuration: MinHeapFreeRatio = 0 MaxHeapFreeRatio = 100 MaxHeapSize = 8575254528 (8178.0MB) NewSize = 178782208 (170.5MB) MaxNewSize = 2858418176 (2726.0MB) OldSize = 358088704 (341.5MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 0 Heap Usage: PS Young Generation Eden Space: Capacity = 624427008 (595.5MB) Used = 32083672 (30.597373962402344MB) Free = 592343336 (564.9026260375977MB) 5.138098062536078%, informs the From Space: Capacity = 19398656 (18.5MB) Used = 15687272 (14.960548400878906MB) Free = 3711384 (3.5394515991210938MB) 80.86782919394004%, informs the To Space: Capacity = 20447232 (19.5MB) Used = 0 (0.0MB) Free = 20447232 (19.5MB) 0.0% Used PS Old Generation Capacity = 256901120 (245.0MB) Used = 22278496 (21.246429443359375MB) Free = 234622624 (223.75357055664062MB) 8.672012017697703% Used 24741 interned Strings occupying 2987512 bytes.Copy the code

First, it prints some relevant configurations of the heap, such as the maximum new generation, the size of the meta-space, etc. The following is the usage of the heap, including Eden, S0, S1 of Cenozoic era and old age.

Jmap can also save heap information as a file snapshot. Run jmap -dump:live,format=b,file=heap.bin 8825.

[root@wupx ~]# jmap -dump:live,format=b,file=heap.bin 8825
Dumping heap to /root/heap.bin ...
Heap dump file created
Copy the code

The heap.bin can be opened using the jhat command and is presented in HTML.

jhat

Jhat analysis Java heap command, can be heap objects in HTML form display, support object query language OQL, command content as follows:

Now run jhat-port 9999 heap.bin to display the heap.bin file as HTML:

[root@wupx ~]# jhat -port 9999 heap.bin
Reading from heap.bin...
Dump file created Tue May 12 22:31:55 CST 2020
Snapshot read, resolving... Resolving 570997 objects... Chasing references, expect 114 dots.................................................................................................................. Eliminating duplicate references.............................................................................................................. . Snapshot resolved. Started HTTP server on port 9999 Server is ready.Copy the code

After executing, open http://localhost:9999/ to see the heap usage of instances of the class, grouped by package name:

There are a number of other ways to Query at the bottom of the page:

For example, enter select s from java.lang.String s where s.value. Length >= 100 to query instances whose String length is greater than 100.

jstack

Jstack is a stack tracing tool, which is mainly used to print the stack information of the PID of a given process. It is usually used to troubleshoot problems when deadlocks occur or the CPU reaches 100%. You can query the current running thread and the stack information of the thread.

Run jstack -f 8825 > jstack.log to save the thread information.

Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.161-b12 mixed mode):

"Attach Listener" #51805777 daemon prio=9 os_prio=0 tid=0x00007f971c001000 nid=0x9cd6 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"DestroyJavaVM" #55 prio=5 os_prio=0 tid=0x00007f9fc8009800 nid=0x227a waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"http-nio-8111-Acceptor-0" #52 daemon prio=5 os_prio=0 tid=0x00007f96c40c5800 nid=0x2653 runnable [0x00007f97c0df9000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
	at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422)
	at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250)
	- locked <0x00007f982c6213c8> (a java.lang.Object)
	at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:455)
	at java.lang.Thread.run(Thread.java:748)

"http-nio-8111-ClientPoller-0" #50 daemon prio=5 os_prio=0 tid=0x00007f9fc8e7e000 nid=0x2651 runnable [0x00007f97c21fb000]
   java.lang.Thread.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 <0x00007f982c622460> (a sun.nio.ch.Util$3)
	- locked <0x00007f982c622450> (a java.util.Collections$UnmodifiableSet) - locked <0x00007f982c622408> (a sun.nio.ch.EPollSelectorImpl) at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)  at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:787)
	at java.lang.Thread.run(Thread.java:748)

"Service Thread" #17 daemon prio=9 os_prio=0 tid=0x00007f9fc8379000 nid=0x229d runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread10" #15 daemon prio=9 os_prio=0 tid=0x00007f9fc8373800 nid=0x229b waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"VM Thread" os_prio=0 tid=0x00007f9fc831c000 nid=0x228d runnable 

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f9fc801e800 nid=0x227b runnable 

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f9fc8020800 nid=0x227c runnable 

"VM Periodic Task Thread" os_prio=0 tid=0x00007f9fc837e000 nid=0x229e waiting on condition 

JNI global references: 357
Copy the code

ParallelGC is a parallel collector (ParallelGC is a parallel collector, with a default of 2 threads). ParallelGC is a parallel collector (ParallelGC is a parallel collector, with a default of 2 threads). ParallelGC is a parallel collector (ParallelGC is a parallel collector). You can see how many threads are in the RUNNABLE state. Usually, the CPU is too high because there are too many threads in the RUNNABLE state. If a lot of threads are in TIMED_WAITING state, theoretically the CPU usage should not be very high.

conclusion

This article mainly introduces the common JDK built-in commands javap, JPS, jstat, JCMD, jmap, jhat, jstack, you can practice on the machine.

Knowing these commands will be of great help in troubleshooting deadlocks, high CPU usage, and program performance tuning. In the future, the GRAPHICAL tools of the JDK and troubleshooting examples of high CPU usage will be introduced.

The best relationship is mutual achievement. The three links of watching, forwarding and leaving messages are the biggest motivation for my creation.

reference

Docs.oracle.com/javase/8/do…

JDK built-in command tools