JDK命令-jcmd

最后更新:2020-04-24

用来打印Java进程所涉及的基本类、线程和VM信息

	Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>
	   or: jcmd -l                                                    
	   or: jcmd -h                                                    
	                                                                  
	  command must be a valid jcmd command for the selected jvm.      
	  Use the command "help" to see which commands are available.   
	  If the pid is 0, commands will be sent to all Java processes.   
	  The main class argument will be used to match (either partially 
	  or fully) the class used to start Java.                         
	  If no options are given, lists Java processes (same as -p).     
	                                                                  
	  PerfCounter.print display the counters exposed by this process  
	  -f  read and execute commands from the file                     
	  -l  list JVM processes on the local machine                     
	  -h  this help  

PerfCounter.print

打印目标进程的性能计数器

- file

从文件file中读取命令,然后在目标Java进程上调用这些命令。在file中,每个命令必须写在单独的一行。 以”#”开头的行会被忽略。当所有行的命令被调用完毕后,或者读取到含有stop关键字的命令, 将会终止对file的处理。

实例查看当前java进程

	# jcmd -l
	2593 org.apache.zookeeper.server.quorum.QuorumPeerMain /alidata/server/zookeeper-3.5.1-alpha/bin/../conf/zoo.cfg

查看目标jvm中能获取到的信息

	# jcmd 8605 help
	8605:
	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
	GC.rotate_log
	Thread.print
	GC.class_stats
	GC.class_histogram
	GC.heap_dump
	GC.run_finalization
	GC.run
	VM.uptime
	VM.flags
	VM.system_properties
	VM.command_line
	VM.version
	help
	For more information about a specific command use 'help <command>'.

查看上面方法的帮助

	# jcmd 8605 help VM.uptime
	8605:
	VM.uptime
	Print VM uptime.
	
	Impact: Low
	
	Syntax : VM.uptime [options]
	
	Options: (options must be specified using the <key> or <key>=<value> syntax)
		-date : [optional] Add a prefix with current date (BOOLEAN, false)

运行时长

	jcmd 8605 VM.uptime
	8605:
	604705.031 s

系统属性

	# jcmd 8605 VM.system_properties
	8605:
	#Thu Jul 27 16:18:04 CST 2017
	java.runtime.name=Java(TM) SE Runtime Environment
	sun.boot.library.path=/alidata/server/jdk1.8.0_20/jre/lib/amd64
	java.vm.version=25.20-b23
	java.vm.vendor=Oracle Corporation
	java.vendor.url=http\://java.oracle.com/
	path.separator=\:
	java.vm.name=Java HotSpot(TM) 64-Bit Server VM

JVM版本

	# jcmd 8605 VM.version
	8605:
	Java HotSpot(TM) 64-Bit Server VM version 25.20-b23
	JDK 8.0_20

JVM命令行

	# jcmd 8605 VM.command_line
	8605:
	VM Arguments:
	jvm_args: -Xms300m -Xmx300m -XX:NewRatio=1 
	java_command: /alidata/server/v1.2/iotp-app/standalone-1.0.0.jar --iotp.server.id=iotp-app --conf=/alidata/server/v1.2/config/app-api.json -Dvertx.metrics.options.enabled=true -Dvertx.metrics.options.registryName=iotp-app-registry
	java_class_path (initial): /alidata/server/v1.2/iotp-app/standalone-1.0.0.jar
	Launcher Type: SUN_STANDARD

JVM调优标志

	# jcmd 8605 VM.flags
	8605:
	-XX:CICompilerCount=3 -XX:InitialHeapSize=314572800 -XX:MaxHeapSize=314572800 -XX:MaxNewSize=157286400 -XX:MinHeapDeltaBytes=524288 -XX:NewRatio=1 -XX:NewSize=157286400 -XX:OldSize=157286400 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC 
	# jcmd 8605 VM.flags -all
	8605:
	[Global flags]
	    uintx AdaptiveSizeDecrementScaleFactor          = 4                                   {product}
	    uintx AdaptiveSizeMajorGCDecayTimeScale         = 10                                  {product}

输出中的:表示标志使用的非默认值,发生这种情况,可能是以下原因导致:

  • 标志值直接在命令行指定
  • 其他标志间接改变了该标志的值
  • JVM自动优化计算出来的默认值

product表示在所有平台上的默认设置都是一致的。pd product表示标志的默认值是独立于平台的。

     intx CICompilerCount                          := 3                                   {product}
     bool CICompilerCountPerCPU                     = true                                {product}
     bool CITime                                    = false                               {product}
     bool CMSAbortSemantics                         = false                               {product}
    uintx CMSAbortablePrecleanMinWorkPerIteration   = 100                                 {product}
     intx CMSAbortablePrecleanWaitMillis            = 100                                 {manageable}

获取线程的栈信息

	# jcmd 8605 Thread.print
	8605:
	2017-07-27 16:44:58
	Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.20-b23 mixed mode):
	
	"vert.x-eventloop-thread-7" #50 prio=5 os_prio=0 tid=0x00007f65f001e800 nid=0x7cfe runnable [0x00007f664a4f8000]
	   java.lang.Thread.State: RUNNABLE
	   
Edgar

Edgar
一个略懂Java的小菜比