My experiments with jcmd

icmd is recommended java utility JDK8 onwards to troubleshoot issues in java application

Posted by Vipin Sharma on Wednesday, October 18, 2017 Tags: java   9 minute read

For commands given here we will be using JDK 9.0.0.

Most of the commands are valid for JDK8 as well, only VM.set_flag was introduced with jdk 9, JEP-228.

We will start with basic commands to see help and list of java processes.

jcmd -h: Displays the jcmd utility’s command-line help.

  username@users-Air:~/javacode$ jcmd -h
  Usage: jcmd  
  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 -l).     

  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    

jcmd -l: Displays the list of running Java Virtual Machine processes

    username@users-Air:~/javacode$ jcmd -l
    11441 Test
    6294 Test
    29197 jdk.jcmd/sun.tools.jcmd.JCmd -l

Next few minutes I will be using these 2 processes (11441 and 6294) to demonstrate jcmd usages.

List of available help commands for process 6294:

username@users-Air:~/javacode$ jcmd 6294 help
6294:
The following commands are available:
JFR.configure
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.log
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.status
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
Compiler.directives_clear
Compiler.directives_remove
Compiler.directives_add
Compiler.directives_print
VM.print_touched_methods
Compiler.codecache
Compiler.codelist
Compiler.queue
VM.classloader_stats
Thread.print
JVMTI.data_dump
JVMTI.agent_load
VM.stringtable
VM.symboltable
VM.class_hierarchy
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.finalizer_info
GC.heap_info
GC.run_finalization
GC.run
VM.info
VM.uptime
VM.dynlibs
VM.set_flag
VM.flags
VM.system_properties
VM.command_line
VM.version
help

For more information about a specific command use 'help '.

Now we will see how to get more info on some command, lets check more info on VM.set_flag:

username@users-Air:~/javacode$ jcmd 11441 help VM.set_flag
11441:
VM.set_flag
Sets VM flag option using the provided value.

Impact: Low

Permission: java.lang.management.ManagementPermission(control)

Syntax : VM.set_flag   []

Arguments:
  flag name :  The name of the flag we want to set (STRING, no default value)
  string value : [optional] The value we want to set (STRING, no default value)

Now I will try to run some command and share output with you:

VM.version:

username@users-Air:~/javacode$ jcmd 6294 VM.version
6294:
Java HotSpot(TM) 64-Bit Server VM version 9+181
JDK 9.0.0

GC.run:

username@users-Air:~/javacode$ jcmd 6294 GC.run
6294:
Command executed successfully

Getting stack trace using Thread.print

username@users-Air:~/javacode$ jcmd 6294 Thread.print
6294:
2017-10-17 11:28:55
Full thread dump Java HotSpot(TM) 64-Bit Server VM (9+181 mixed mode):

"Attach Listener" #11 daemon prio=9 os_prio=31 tid=0x00007fbcd6883000 nid=0x1407 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Service Thread" #10 daemon prio=9 os_prio=31 tid=0x00007fbcd6870800 nid=0x6103 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Common-Cleaner" #9 daemon prio=8 os_prio=31 tid=0x00007fbcd7063000 nid=0x5f03 in Object.wait() [0x0000700009bc9000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
  at java.lang.Object.wait(java.base@9/Native Method)
  - waiting on <0x0000000780074160> (a java.lang.ref.ReferenceQueue$Lock)
  at java.lang.ref.ReferenceQueue.remove(java.base@9/ReferenceQueue.java:151)
  - waiting to re-lock in wait() <0x0000000780074160> (a java.lang.ref.ReferenceQueue$Lock)
  at jdk.internal.ref.CleanerImpl.run(java.base@9/CleanerImpl.java:148)
  at java.lang.Thread.run(java.base@9/Thread.java:844)
  at jdk.internal.misc.InnocuousThread.run(java.base@9/InnocuousThread.java:122)

"Sweeper thread" #8 daemon prio=9 os_prio=31 tid=0x00007fbcd703e000 nid=0x5d03 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread2" #7 daemon prio=9 os_prio=31 tid=0x00007fbcd703d000 nid=0x5b03 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"C2 CompilerThread1" #6 daemon prio=9 os_prio=31 tid=0x00007fbcd7026000 nid=0x5903 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"C2 CompilerThread0" #5 daemon prio=9 os_prio=31 tid=0x00007fbcd6863000 nid=0x5703 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x00007fbcd6862000 nid=0x5503 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007fbcd7021800 nid=0x4503 in Object.wait() [0x00007000095b7000]
   java.lang.Thread.State: WAITING (on object monitor)
  at java.lang.Object.wait(java.base@9/Native Method)
  - waiting on <0x000000078004e440> (a java.lang.ref.ReferenceQueue$Lock)
  at java.lang.ref.ReferenceQueue.remove(java.base@9/ReferenceQueue.java:151)
  - waiting to re-lock in wait() <0x000000078004e440> (a java.lang.ref.ReferenceQueue$Lock)
  at java.lang.ref.ReferenceQueue.remove(java.base@9/ReferenceQueue.java:172)
  at java.lang.ref.Finalizer$FinalizerThread.run(java.base@9/Finalizer.java:216)

"Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x00007fbcd684c800 nid=0x4303 waiting on condition [0x00007000094b4000]
   java.lang.Thread.State: RUNNABLE
  at java.lang.ref.Reference.waitForReferencePendingList(java.base@9/Native Method)
  at java.lang.ref.Reference.processPendingReferences(java.base@9/Reference.java:174)
  at java.lang.ref.Reference.access$000(java.base@9/Reference.java:44)
  at java.lang.ref.Reference$ReferenceHandler.run(java.base@9/Reference.java:138)

"main" #1 prio=5 os_prio=31 tid=0x00007fbcd7008000 nid=0x1d03 waiting on condition [0x000070000878d000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
  at java.lang.Thread.sleep(java.base@9/Native Method)
  at Test.main(Test.java:5)

"VM Thread" os_prio=31 tid=0x00007fbcd7019000 nid=0x4103 runnable

"GC Thread#0" os_prio=31 tid=0x00007fbcd680a800 nid=0x2903 runnable

"GC Thread#1" os_prio=31 tid=0x00007fbcd680b000 nid=0x2b03 runnable

"GC Thread#2" os_prio=31 tid=0x00007fbcd680b800 nid=0x2d03 runnable

"GC Thread#3" os_prio=31 tid=0x00007fbcd680c800 nid=0x2f03 runnable

"G1 Main Marker" os_prio=31 tid=0x00007fbcd6824000 nid=0x3b03 runnable

"G1 Marker#0" os_prio=31 tid=0x00007fbcd6824800 nid=0x3f03 runnable

"G1 Refine#0" os_prio=31 tid=0x00007fbcd680f000 nid=0x3703 runnable

"G1 Refine#1" os_prio=31 tid=0x00007fbcd680e800 nid=0x3503 runnable

"G1 Refine#2" os_prio=31 tid=0x00007fbcd680d800 nid=0x3303 runnable

"G1 Refine#3" os_prio=31 tid=0x00007fbcd680d000 nid=0x3103 runnable

"G1 Young RemSet Sampling" os_prio=31 tid=0x00007fbcd6810000 nid=0x3903 runnable

"VM Periodic Task Thread" os_prio=31 tid=0x00007fbcd6871800 nid=0x6303 waiting on condition

JNI global references: 6

VM.stringtable

username@users-Air:~/javacode$ jcmd 6294 VM.stringtable
6294:
StringTable statistics:
Number of buckets       :     60013 =    480104 bytes, avg   8.000
Number of entries       :      2838 =     68112 bytes, avg  24.000
Number of literals      :      2838 =    192120 bytes, avg  67.696
Total footprint         :           =    740336 bytes
Average bucket size     :     0.047
Variance of bucket size :     0.047
Std. dev. of bucket size:     0.217
Maximum bucket size     :         3

jcmd 11441 VM.symboltable:

username@users-Air:~/javacode$ jcmd 11441 VM.symboltable
11441:
SymbolTable statistics:
Number of buckets       :     20011 =    160088 bytes, avg   8.000
Number of entries       :     26800 =    643200 bytes, avg  24.000
Number of literals      :     26800 =   1008920 bytes, avg  37.646
Total footprint         :           =   1812208 bytes
Average bucket size     :     1.339
Variance of bucket size :     1.322
Std. dev. of bucket size:     1.150
Maximum bucket size     :         8

GC.heap_info

username@users-Air:~/javacode$ jcmd 6294 GC.heap_info
6294:
 garbage-first heap   total 8192K, used 779K [0x0000000780000000, 0x0000000780100040, 0x00000007c0000000)
  region size 1024K, 0 young (0K), 0 survivors (0K)
 Metaspace       used 3995K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 355K, capacity 386K, committed 512K, reserved 1048576K

GC.heap_dump:

username@users-Air:~/javacode$ jcmd 6294 GC.heap_dump -all=true mydump.dump
6294:
Heap dump file created
username@users-Air:~/javacode$ ls -ltr
total 12528
-rw-r--r--  1 username  staff      199 Oct 17 11:21 Test.java~
-rw-r--r--  1 username  staff      202 Oct 17 11:22 Test.java
-rw-r--r--  1 username  staff      565 Oct 17 11:22 Test.class
-rw-------  1 username  staff  2133171 Oct 17 11:46 mydump.dump

jcmd 11441(pid) VM.flags:

username@users-Air:~/javacode$ jcmd 11441 VM.flags
11441:
-XX:CICompilerCount=3 -XX:ConcGCThreads=1 -XX:G1ConcRefinementThreads=4 -XX:G1HeapRegionSize=1048576 -XX:InitialHeapSize=67108864 -XX:MarkStackSize=4194304 -XX:MaxHeapSize=1073741824 -XX:MaxNewSize=643825664 -XX:MinHeapDeltaBytes=1048576 -XX:NonNMethodCodeHeapSize=5830092 -XX:NonProfiledCodeHeapSize=122914074 -XX:ProfiledCodeHeapSize=122914074 -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:-UseAOT -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseG1GC

Similar command using jinfo:

username@users-Air:~/javacode$ jinfo -flags 11441
VM Flags:
-XX:CICompilerCount=3 -XX:ConcGCThreads=1 -XX:G1ConcRefinementThreads=4 -XX:G1HeapRegionSize=1048576 -XX:InitialHeapSize=67108864 -XX:MarkStackSize=4194304 -XX:MaxHeapSize=1073741824 -XX:MaxNewSize=643825664 -XX:MinHeapDeltaBytes=1048576 -XX:NonNMethodCodeHeapSize=5830092 -XX:NonProfiledCodeHeapSize=122914074 -XX:ProfiledCodeHeapSize=122914074 -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:-UseAOT -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseG1GC

jcmd 11441(pid) VM.system_properties:

username@users-Air:~/javacode$ jcmd 11441 VM.system_properties
11441:
Tue Oct 17 12:44:50 IST 2017
gopherProxySet=false
awt.toolkit=sun.lwawt.macosx.LWCToolkit
file.encoding.pkg=sun.io
java.specification.version=9
sun.cpu.isalist=
sun.jnu.encoding=UTF-8
java.class.path=.
java.vm.vendor=Oracle Corporation
sun.arch.data.model=64
java.vendor.url=http://java.oracle.com/
user.timezone=Asia/Kolkata
java.vm.specification.version=9
os.name=Mac OS X
sun.java.launcher=SUN_STANDARD
user.country=US
sun.boot.library.path=/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/lib
sun.java.command=Test
http.nonProxyHosts=local|*.local|169.254/16|*.169.254/16
jdk.debug=release
sun.cpu.endian=little
user.home=/Users/username
user.language=en
java.specification.vendor=Oracle Corporation
java.home=/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
file.separator=/
java.vm.compressedOopsMode=Zero based
line.separator=n
java.specification.name=Java Platform API Specification
java.vm.specification.vendor=Oracle Corporation
java.awt.graphicsenv=sun.awt.CGraphicsEnvironment
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
ftp.nonProxyHosts=local|*.local|169.254/16|*.169.254/16
java.runtime.version=9+181
user.name=username
path.separator=:
os.version=10.12.6
java.runtime.name=Java(TM) SE Runtime Environment
file.encoding=UTF-8
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
java.vendor.url.bug=http://bugreport.java.com/bugreport/
java.io.tmpdir=/var/folders/dm/gd6lc90d0hg220lzw_m7krr00000gn/T/
java.version=9
user.dir=/Users/username/javacode
os.arch=x86_64
java.vm.specification.name=Java Virtual Machine Specification
java.awt.printerjob=sun.lwawt.macosx.CPrinterJob
sun.os.patch.level=unknown
MyParam=2
java.library.path=/Users/username/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
java.vm.info=mixed mode
java.vendor=Oracle Corporation
java.vm.version=9+181
sun.io.unicode.encoding=UnicodeBig
java.class.version=53.0
socksNonProxyHosts=local|*.local|169.254/16|*.169.254/16

Similar output using jinfo:

username@users-Air:~/javacode$ jinfo -sysprops 11441
Java System Properties:
Tue Oct 17 12:41:26 IST 2017
gopherProxySet=false
awt.toolkit=sun.lwawt.macosx.LWCToolkit
file.encoding.pkg=sun.io
java.specification.version=9
sun.cpu.isalist=
sun.jnu.encoding=UTF-8
java.class.path=.
java.vm.vendor=Oracle Corporation
sun.arch.data.model=64
java.vendor.url=http://java.oracle.com/
user.timezone=
java.vm.specification.version=9
os.name=Mac OS X
sun.java.launcher=SUN_STANDARD
user.country=US
sun.boot.library.path=/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/lib
sun.java.command=Test
http.nonProxyHosts=local|*.local|169.254/16|*.169.254/16
jdk.debug=release
sun.cpu.endian=little
user.home=/Users/username
user.language=en
java.specification.vendor=Oracle Corporation
java.home=/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
file.separator=/
java.vm.compressedOopsMode=Zero based
line.separator=n
java.specification.name=Java Platform API Specification
java.vm.specification.vendor=Oracle Corporation
java.awt.graphicsenv=sun.awt.CGraphicsEnvironment
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
ftp.nonProxyHosts=local|*.local|169.254/16|*.169.254/16
java.runtime.version=9+181
user.name=username
path.separator=:
os.version=10.12.6
java.runtime.name=Java(TM) SE Runtime Environment
file.encoding=UTF-8
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
java.vendor.url.bug=http://bugreport.java.com/bugreport/
java.io.tmpdir=/var/folders/dm/gd6lc90d0hg220lzw_m7krr00000gn/T/
java.version=9
user.dir=/Users/username/javacode
os.arch=x86_64
java.vm.specification.name=Java Virtual Machine Specification
java.awt.printerjob=sun.lwawt.macosx.CPrinterJob
sun.os.patch.level=unknown
MyParam=2
java.library.path=/Users/username/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
java.vm.info=mixed mode
java.vendor=Oracle Corporation
java.vm.version=9+181
sun.io.unicode.encoding=UnicodeBig
java.class.version=53.0
socksNonProxyHosts=local|*.local|169.254/16|*.169.254/16

VM.set_flag

username@users-Air:~/javacode$ jcmd 6294 VM.set_flag ConcGCThreads 4
6294:
only 'writeable' flags can be set
username@users-Air:~/javacode$ jcmd 6294 VM.set_flag MaxNewSize 1G
6294:
only 'writeable' flags can be set

As you can see my commands failed because only manageable field can be set using VM.set_flag, lets see same command over manageable flag

user@users-Air:~/javacode$ jcmd 11441 VM.flags -all | grep MinHeapFreeRatio
    uintx MinHeapFreeRatio             = 40         {manageable} {default}
user@users-Air:~/javacode$ jcmd 11441 VM.set_flag MinHeapFreeRatio 45
11441:
Command executed successfully
user@users-Air:~/javacode$ jcmd 11441 VM.flags -all | grep MinHeapFreeRatio
    uintx MinHeapFreeRatio             = 45       {manageable} {management}

At the end

To learn the best java language features and get the best Java Jobs, download my ebook 5 steps to Best Java Jobs for Free.

Follow me on twitter @vipinbit to get daily tips like this on Java Language Features.