Article Directory
1. Function introduction
In JDK1.7
later added a command-line tool jcmd
with which you can view the stack information, see the Java process, export thread information, perform GC, may also sampling and analysis (similar to the flight recorders jmc tool), you can see it as jps/jmap/jstack
other tools Collection of
2. Detailed explanation of jcmd command
2.1 Command format
In use the terminal interface jcmd -h
is readily available help documentation of the command, the command format is as follows
Command format | Description |
---|---|
jcmd < pid | main class> <command …| PerfCounter.print | -f file> | The most commonly used command format, with multiple parameters |
jcmd -l | Equivalent to jps command |
- The meanings of the parameters of commonly used jcmd commands are as follows
parameter meaning pid ID of the process that received the diagnostic command request main class The main class of the process that receives the diagnostic command request. When matching processes, any process that contains the specified substring in the main class name is matched. If multiple running Java processes share the same main class, the diagnostic command request will be sent to all these processes command It must be a valid jcmd command. If the specified pid is 0, the command will be sent to all Java processes. Note that if the parameter contains spaces, it must be enclosed in English single or double quotation marks. In addition, useEscape characters \
to escape single or double quotes in parameters to prevent the operating system shell from processing these quote marksPerfcounter.print Print performance statistics on the target Java process, this list may change with the Java process -f file Read commands from the file file, and then call these commands on the target Java process. Each command in file must be on a separate line, and lines beginning with "#" will be ignored. When all lines of command is invoked to read or finish containing the stop
command keywords, the process is terminated
2.2 Detailed explanation of common command parameters
Use jcmd <pid> help
command to view for a specified PID can call command
parameters, as follows conventional print. See generally includes JFR
GC
VM
three categories
// 执行的命令
jcmd 1715 help
// 对进程 1715 可用的 command 参数
1715:
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
2.2.1 GC prefix
GC
The prefix command parameter is mainly used to view information related to garbage collection in the heap. The parameter table is as follows
parameter | Features |
---|---|
GC.rotate_log | gc log rotation command, use this command to force the creation of a new gc log file |
GC.class_stats | To print more detailed information of the loaded class, parameters need to be enabled -XX:+UnlockDiagnosticVMOptions |
GC.class_histogram | View the number of instances of each class in the heap and the memory space occupied |
GC.heap_dump | Export Heap Dump of JVM,If only the file name is specified, it will be generated in the directory where the JVM is started by default |
GC.finalizer_info | Information about objects waiting to be recycled |
GC.heap_info | Print brief information about the heap, including the GC algorithm used and heap configuration information |
Thread.print | Print thread stack information, similar jstack -l |
GC.run_finalization | Execute java.lang.System.runFinalization() to force the finalize method of the object whose reference has been lost to be called |
GC.run | Execute java.lang.System.gc(), JVM will perform Full GC at the right time |
2.2.2 VM prefix
VM
The prefix command parameter is mainly used to view information related to the virtual machine. The parameter table and functions are as follows
parameter | Features |
---|---|
VM.native_memory | Print the memory usage of the entire Java process, with parameters enabled-XX:NativeMemoryTracking=[summary | detail] |
VM.check_commercial_features | View business functions, before using the need to use VM.unlock_commercial_features Unlock business functions |
VM.classloader_stats | Print class loader information |
VM.uptime | View the virtual machine startup time |
VM.dynlibs | Print dynamic link library information |
VM.flags | View virtual machine startup parameters |
VM.system_properties | View the attribute information of the JVM |
VM.command_line | View start command |
VM.version | View the virtual machine version |
2.2.4 JFR prefix
JRF
With the function jmc工具
of the flight recorder function the same, using the relevant functions must use VM.unlock_commercial_features
parameter Unlock business functions.Execute JFR
the command to dump jfr files that need to be imported into jmc visual analysis tool to do
parameter | Features |
---|---|
JFR.start name=hello duration=120s | Start JFR |
JFR.check name=hello duration=120s | Check JFR status |
JFR.stop name=hello duration=120s | Stop JFR |
JFR.dump name=hello duration=120s filename=hello.jfr (file name must be .jfr suffix) | After waiting at least duration (120s is set here), execute the command dump to output the file |
3. Examples of use
3.1 GC related commands
-
jcmd 22852 GC.class_stats | more
The printed content is as follows, the more key information isThe name of the loaded class (ClassName), the bytes occupied by each class (KlassBytes), the bytes occupied by instances of each class (InstBytes), the number of methods in each class (MethodCount), and the bytes occupied by Space (ByteCodes))22852: Index Super InstBytes KlassBytes annotations CpAll MethodCount Bytecodes MethodAll ROAll RWAll Total ClassName 1 -1 8159824 480 0 0 0 0 0 24 584 608 [C 2 16 2508576 568 0 1344 8 223 1744 1024 2952 3976 java.util.concurrent.ConcurrentHashMap$Node 3 -1 2266352 480 0 0 0 0 0 24 584 608 [B 4 8253 2177296 1112 0 5512 40 847 20528 4224 23608 27832 java.lang.reflect.Method 5 16 2023608 624 0 8712 94 4623 52496 12136 50776 62912 java.lang.String 6 16 1595480 648 0 19384 130 4973 68112 16552 73368 89920 java.lang.Class 7 -1 1147856 480 0 0 0 0 0 24 584 608 [Ljava.lang.Object; 8 16 958656 632 0 2280 10 248 4760 1640 6400 8040 org.aspectj.weaver.reflect.ShadowMatchImpl 9 -1 912752 480 0 0 0 0 0 24 584 608 [I 10 13 819080 560 0 384 1 10 496 232 1432 1664 java.util.LinkedHashMap$Entry ......
-
jcmd 22852 GC.heap_info
Print heap memory usage information22852: // G1 收集器,堆使用情况 garbage-first heap total 106496K, used 31763K [0x00000000f8000000, 0x00000000f8100340, 0x0000000100000000) // G1 region 的大小,年轻代有 1个 region,survivors 0 个 region region size 1024K, 1 young (1024K), 0 survivors (0K) // 元空间使用情况 Metaspace used 73058K, capacity 75244K, committed 75976K, reserved 1116160K class space used 9071K, capacity 9493K, committed 9672K, reserved 1048576K
-
jcmd 22852 Thread.print | less
Print thread stack, with thejstack -l 22852
same effect22852: 2020-04-08 18:10:06 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode): "logback-8" #78 daemon prio=5 os_prio=0 tid=0x00007f4f54001000 nid=0x63f2 waiting on condition [0x00007f4f993e8000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x00000000f840cd38> (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.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088) at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) .......
3.2 VM related commands
VM
The most important of the related commands is VM.native_memory
that this command is mainly used to view the memory usage of the entire Java process, and can also be tracked and detected if necessary. jcm < pid > VM.native_memory scale=MB
The output can refer to JVM direct memory ,Here mainly introduces the commands that can be used to track memory leaks
jcmd 22852 VM.native_memory baseline
Create a baseline based on the current memory usage of the Java processjcmd 22852 VM.native_memory summary.diff scale=KB
运行一段时间后使用summary.diff
来查看跟baseline
对比的统计信息jcmd 22852 VM.native_memory shutdown
可以用于关闭 NMT,但是关闭之后没有对应 jcmd 命令来开启22852: Native Memory Tracking: // 统计情况看,保留的内存增加了 37K,实际使用的内存增加了 293K Total: reserved=1642215KB +37KB, committed=349111KB +293KB - Java Heap (reserved=131072KB, committed=106496KB) (mmap: reserved=131072KB, committed=106496KB) - Class (reserved=1118433KB, committed=78505KB +256KB) (classes #13496) (malloc=2273KB #18090 +10) (mmap: reserved=1116160KB, committed=76232KB +256KB) - Thread (reserved=55638KB, committed=55638KB) (thread #55) (stack: reserved=55404KB, committed=55404KB) (malloc=170KB #282) (arena=63KB #104) - Code (reserved=253929KB +34KB, committed=26497KB +34KB) (malloc=4329KB +34KB #6590 +52) (mmap: reserved=249600KB, committed=22168KB) - GC (reserved=55718KB +1KB, committed=54806KB +1KB) (malloc=18086KB +1KB #12432 +49) (mmap: reserved=37632KB, committed=36720KB) - Compiler (reserved=165KB, committed=165KB) (malloc=35KB #530) (arena=131KB #7) - Internal (reserved=3285KB, committed=3285KB) (malloc=3253KB #19786 +2) (mmap: reserved=32KB, committed=32KB) - Symbol (reserved=19850KB, committed=19850KB) (malloc=17029KB #176152) (arena=2821KB #1) - Native Memory Tracking (reserved=3671KB +2KB, committed=3671KB +2KB) (malloc=12KB #137) (tracking overhead=3660KB +2KB) - Arena Chunk (reserved=198KB, committed=198KB) (malloc=198KB) - Unknown (reserved=256KB, committed=0KB) (mmap: reserved=256KB, committed=0KB)