jcmdを試す

jcmdを試してみたメモです。

java7から追加されたユーティリティのjcmdを試してみたメモです。
他のユーティリティで対応するコマンドがあれば比較してみました。

(2017/10/02) jmapのliveオプションについて追記
---

実行中のJVMプロセス表示

jcmd

引数なしで実行すると実行中のJVMプロセスを表示します。

$ jcmd
60630 org.jetbrains.idea.maven.server.RemoteMavenServer
60649 sun.tools.jcmd.JCmd
60603
60636 org.jetbrains.jps.cmdline.Launcher /Applications/IntelliJ IDEA CE.app/Contents/lib/util.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/httpclient-4.5.2.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/jna-platform.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/log4j.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/maven-aether-provider-3.3.9-all.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/resources_en.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/openapi.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/commons-codec-1.9.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/jps-model.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/lz4-java-1.3.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/nanoxml-2.2.3.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/netty-all-4.1.10.Final.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/jgoodies-forms.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/aether-1.1.0-all.j
60637 com.example.propagation.transaction.transactionpropagationexample.TransactionPropagationExampleApplication

jps -ml

jpsだと-mオプション(mainメソッドの引数)と-lオプション(フルパッケージ名)を付けた場合と一緒になりました。

$ jps -ml
60630 org.jetbrains.idea.maven.server.RemoteMavenServer
60603
60715 sun.tools.jps.Jps -ml
60636 org.jetbrains.jps.cmdline.Launcher /Applications/IntelliJ IDEA CE.app/Contents/lib/util.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/httpclient-4.5.2.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/jna-platform.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/log4j.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/maven-aether-provider-3.3.9-all.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/resources_en.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/openapi.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/commons-codec-1.9.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/jps-model.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/lz4-java-1.3.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/nanoxml-2.2.3.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/netty-all-4.1.10.Final.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/jgoodies-forms.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/aether-1.1.0-all.j
60637 com.example.propagation.transaction.transactionpropagationexample.TransactionPropagationExampleApplication
JVMの実行時間を表示

jcmd <pid> VM.uptime

VM.uptimeで対象プロセスの実行時間を表示します。

$ jcmd 33220 VM.uptime
33220:
125.074 s
システムプロパティを表示

jcmd <pid> VM.system_properties

VM.system_propertiesで対象プロセスのシステムプロパティを表示します。

$ jcmd 33220 VM.system_properties
33220:
#Tue Sep 05 19:49:17 JST 2017
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib
java.vm.version=25.131-b11
gopherProxySet=false
java.vm.vendor=Oracle Corporation
java.vendor.url=http\://java.oracle.com/
path.separator=\:
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
user.country=JP
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=unknown
java.vm.specification.name=Java Virtual Machine Specification
user.dir=/Users/abcdefg/Desktop/my_git/spring_examples/transaction-propagation-example
PID=33220
java.runtime.version=1.8.0_131-b11
java.awt.graphicsenv=sun.awt.CGraphicsEnvironment
java.endorsed.dirs=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/endorsed
os.arch=x86_64
org.jboss.logging.provider=slf4j
java.io.tmpdir=/var/folders/2b/j73cpspj0qj7226brkyy2hc80000gp/T/
line.separator=\n
java.vm.specification.vendor=Oracle Corporation
os.name=Mac OS X
sun.jnu.encoding=UTF-8
java.library.path=/Users/abcdefg/Library/Java/Extensions\:/Library/Java/Extensions\:/Network/Library/Java/Extensions\:/System/Library/Java/Extensions\:/usr/lib/java\:.
spring.beaninfo.ignore=true
java.specification.name=Java Platform API Specification
java.class.version=52.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=10.12.6
http.nonProxyHosts=local|*.local|169.254/16|*.169.254/16
user.home=/Users/abcdefg
        :
        :
        :

jinfo -sysprops <pid>

jinfoでは-syspropsオプションでシステムプロパティを表示します。

$ jinfo -sysprops 69697
Attaching to process ID 69697, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.131-b11
java.runtime.name = Java(TM) SE Runtime Environment
java.vm.version = 25.131-b11
sun.boot.library.path = /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib
gopherProxySet = false
java.vendor.url = http://java.oracle.com/
java.vm.vendor = Oracle Corporation
path.separator = :
file.encoding.pkg = sun.io
java.vm.name = Java HotSpot(TM) 64-Bit Server VM
sun.os.patch.level = unknown
sun.java.launcher = SUN_STANDARD
user.country = JP
user.dir = /Users/abcdefg/Desktop/my_git/spring_examples/transaction-propagation-example
java.vm.specification.name = Java Virtual Machine Specification
PID = 69697
java.runtime.version = 1.8.0_131-b11
java.awt.graphicsenv = sun.awt.CGraphicsEnvironment
os.arch = x86_64
java.endorsed.dirs = /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/endorsed
org.jboss.logging.provider = slf4j
line.separator =
java.io.tmpdir = /var/folders/2b/j73cpspj0qj7226brkyy2hc80000gp/T/
java.vm.specification.vendor = Oracle Corporation
os.name = Mac OS X
sun.jnu.encoding = UTF-8
java.library.path = /Users/abcdefg/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
spring.beaninfo.ignore = true
java.class.version = 52.0
java.specification.name = Java Platform API Specification
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
os.version = 10.12.6
http.nonProxyHosts = local|*.local|169.254/16|*.169.254/16
user.home = /Users/abcdefg
        :
        :
        :
JVMバージョン

jcmd <pid> VM.version

VM.versionで対象プロセスのJVMバージョンを表示

$ jcmd 33220 VM.version
33220:
Java HotSpot(TM) 64-Bit Server VM version 25.131-b11
JDK 8.0_131
JVMコマンドライン引数

jcmd <pid> VM.command_line

VM.command_lineで対象プロセスのJVMコマンドライン引数を表示

$ jcmd 72311 VM.command_line
72311:
VM Arguments:
jvm_args: -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=65242:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8
java_command: com.example.propagation.transaction.transactionpropagationexample.TransactionPropagationExampleApplication
java_class_path (initial): /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
Launcher Type: SUN_STANDARD

jps -v <pid>

jpsでは-vでJVMの引数を表示します。
jcmd VM.command_lineでのjvm_argsに表示されているものが表示されてます。

$ jps -v
72311 TransactionPropagationExampleApplication -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=65242:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8
有効化されているフラグ表示

jcmd <pid> VM.flags

VM.flagsで対象プロセスのJVMで有効化されているフラグを出力

$ jcmd 72311 VM.flags
72311:
-XX:CICompilerCount=4 -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:MaxNewSize=1431306240 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=89128960 -XX:OldSize=179306496 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC

-allを付けるとすべてのフラグを出力

$ jcmd 72311 VM.flags -all
72311:
[Global flags]
    uintx AdaptiveSizeDecrementScaleFactor          = 4                                   {product}
    uintx AdaptiveSizeMajorGCDecayTimeScale         = 10                                  {product}
    uintx AdaptiveSizePausePolicy                   = 0                                   {product}
    uintx AdaptiveSizePolicyCollectionCostMargin    = 50                                  {product}
    uintx AdaptiveSizePolicyInitializingSteps       = 20                                  {product}
    uintx AdaptiveSizePolicyOutputInterval          = 0                                   {product}
    uintx AdaptiveSizePolicyWeight                  = 10                                  {product}
    uintx AdaptiveSizeThroughPutPolicy              = 0                                   {product}
    uintx AdaptiveTimeWeight                        = 25                                  {product}
     bool AdjustConcurrency                         = false                               {product}
     bool AggressiveOpts                            = false                               {product}
     intx AliasLevel                                = 3                                   {C2 product}
     bool AlignVector                               = false                               {C2 product}
     intx AllocateInstancePrefetchLines             = 1                                   {product}
     intx AllocatePrefetchDistance                  = 192                                 {product}
     intx AllocatePrefetchInstr                     = 3                                   {product}
     intx AllocatePrefetchLines                     = 4                                   {product}
     intx AllocatePrefetchStepSize                  = 64                                  {product}
     intx AllocatePrefetchStyle                     = 1                                   {product}
     bool AllowJNIEnvProxy                          = false                               {product}
     bool AllowNonVirtualCalls                      = false                               {product}
     bool AllowParallelDefineClass                  = false                               {product}
     bool AllowUserSignalHandlers                   = false                               {product}
     bool AlwaysActAsServerClassMachine             = false                               {product}
     bool AlwaysCompileLoopMethods                  = false                               {product}
     bool AlwaysLockClassLoader                     = false                               {product}
     bool AlwaysPreTouch                            = false                               {product}
     bool AlwaysRestoreFPU                          = false                               {product}
     bool AlwaysTenure                              = false                               {product}
                :
                :
                :
スレッドダンプ出力

jcmd <pid> Thread.print

Thread.printで対象プロセスのスレッドダンプ出力

$ jcmd 33220 Thread.print
33220:
2017-09-05 19:52:06
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode):

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

"DestroyJavaVM" #34 prio=5 os_prio=31 tid=0x00007f85f817f000 nid=0x1c03 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"http-nio-8080-AsyncTimeout" #32 daemon prio=5 os_prio=31 tid=0x00007f85f9210800 nid=0x7d03 waiting on condition [0x0000700002a1c000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:1200)
    at java.lang.Thread.run(Thread.java:748)

"http-nio-8080-Acceptor-0" #31 daemon prio=5 os_prio=31 tid=0x00007f85f9268000 nid=0x7b03 runnable [0x0000700002919000]
   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 <0x000000076f1dd610> (a java.lang.Object)
    at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:453)
    at java.lang.Thread.run(Thread.java:748)

"http-nio-8080-ClientPoller-1" #30 daemon prio=5 os_prio=31 tid=0x00007f85f538f800 nid=0x7903 runnable [0x0000700002816000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:117)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076f2927d8> (a sun.nio.ch.Util$3)
    - locked <0x000000076f2927c8> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076f2926a8> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:785)
    at java.lang.Thread.run(Thread.java:748)

    :
    :
    :

"VM Thread" os_prio=31 tid=0x00007f85f4809800 nid=0x3503 runnable

"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007f85f4806800 nid=0x2503 runnable

"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007f85f5012000 nid=0x2703 runnable

"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007f85f5013000 nid=0x2903 runnable

"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007f85f6000000 nid=0x2b03 runnable

"GC task thread#4 (ParallelGC)" os_prio=31 tid=0x00007f85f6001000 nid=0x2d03 runnable

"GC task thread#5 (ParallelGC)" os_prio=31 tid=0x00007f85f6001800 nid=0x2f03 runnable

"GC task thread#6 (ParallelGC)" os_prio=31 tid=0x00007f85f6002000 nid=0x3103 runnable

"GC task thread#7 (ParallelGC)" os_prio=31 tid=0x00007f85f6002800 nid=0x3303 runnable

"VM Periodic Task Thread" os_prio=31 tid=0x00007f85f4838000 nid=0x5703 waiting on condition

JNI global references: 284

jstack <pid>

jstackで対象プロセスのスレッドダンプ出力

$ jstack 33220
2017-09-05 19:52:48
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode):

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

"DestroyJavaVM" #34 prio=5 os_prio=31 tid=0x00007f85f817f000 nid=0x1c03 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"http-nio-8080-AsyncTimeout" #32 daemon prio=5 os_prio=31 tid=0x00007f85f9210800 nid=0x7d03 waiting on condition [0x0000700002a1c000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:1200)
    at java.lang.Thread.run(Thread.java:748)

"http-nio-8080-Acceptor-0" #31 daemon prio=5 os_prio=31 tid=0x00007f85f9268000 nid=0x7b03 runnable [0x0000700002919000]
   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 <0x000000076f1dd610> (a java.lang.Object)
    at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:453)
    at java.lang.Thread.run(Thread.java:748)

"http-nio-8080-ClientPoller-1" #30 daemon prio=5 os_prio=31 tid=0x00007f85f538f800 nid=0x7903 runnable [0x0000700002816000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:117)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076f2927d8> (a sun.nio.ch.Util$3)
    - locked <0x000000076f2927c8> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076f2926a8> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:785)
    at java.lang.Thread.run(Thread.java:748)

    :
    :
    :

"VM Thread" os_prio=31 tid=0x00007f85f4809800 nid=0x3503 runnable

"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007f85f4806800 nid=0x2503 runnable

"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007f85f5012000 nid=0x2703 runnable

"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007f85f5013000 nid=0x2903 runnable

"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007f85f6000000 nid=0x2b03 runnable

"GC task thread#4 (ParallelGC)" os_prio=31 tid=0x00007f85f6001000 nid=0x2d03 runnable

"GC task thread#5 (ParallelGC)" os_prio=31 tid=0x00007f85f6001800 nid=0x2f03 runnable

"GC task thread#6 (ParallelGC)" os_prio=31 tid=0x00007f85f6002000 nid=0x3103 runnable

"GC task thread#7 (ParallelGC)" os_prio=31 tid=0x00007f85f6002800 nid=0x3303 runnable

"VM Periodic Task Thread" os_prio=31 tid=0x00007f85f4838000 nid=0x5703 waiting on condition

JNI global references: 284
ヒストグラム表示

jcmd <pid> GC.class_histogram

GC.class_histogramで対象プロセスのヒストグラムを表示

$ jcmd 75019 GC.class_histogram
75019:

 num     #instances         #bytes  class name
----------------------------------------------
   1:         39952        6838208  [C
   2:         16004        1408352  java.lang.reflect.Method
   3:         38902         933648  java.lang.String
   4:          6166         687952  java.lang.Class
   5:         16509         528288  java.util.concurrent.ConcurrentHashMap$Node
   6:          8504         340160  java.util.LinkedHashMap$Entry
   7:          5642         306936  [Ljava.lang.Object;
   8:         13221         289608  [Ljava.lang.Class;
   9:          3563         268144  [Ljava.util.HashMap$Node;
  10:          8376         268032  java.util.HashMap$Node
  11:          4161         233016  java.util.LinkedHashMap
  12:          1198         221696  [B
  13:          2346         168912  java.lang.reflect.Field
  14:           125         164288  [Ljava.util.concurrent.ConcurrentHashMap$Node;
  15:          9511         152176  java.lang.Object
  16:          1807         115384  [Ljava.lang.reflect.Method;
  17:          1251         100080  java.lang.reflect.Constructor
  18:          2449          95224  [I
  19:          2052          93800  [Ljava.lang.String;
  20:          3669          88056  org.springframework.core.MethodClassKey
  21:          1935          77400  java.lang.ref.SoftReference
  22:           781          74976  org.springframework.beans.GenericTypeAwarePropertyDescriptor
  23:          1233          69048  java.lang.Class$ReflectionData
  24:          1854          59328  java.util.LinkedList
  25:          1846          59072  java.lang.ref.WeakReference
  26:          1230          59040  java.util.HashMap
  27:          2191          52584  java.util.ArrayList
  28:          1201          48040  java.util.TreeMap$Entry
  29:          1980          47520  java.beans.MethodRef
  30:           894          42912  org.apache.tomcat.util.modeler.AttributeInfo
        :
        :
        :
        :
2420:             1             16  sun.reflect.GeneratedMethodAccessor9
2421:             1             16  sun.reflect.GeneratedSerializationConstructorAccessor1
2422:             1             16  sun.reflect.GeneratedSerializationConstructorAccessor2
2423:             1             16  sun.reflect.GeneratedSerializationConstructorAccessor3
2424:             1             16  sun.reflect.ReflectionFactory
2425:             1             16  sun.reflect.generics.tree.BooleanSignature
2426:             1             16  sun.reflect.generics.tree.BottomSignature
2427:             1             16  sun.reflect.generics.tree.ByteSignature
2428:             1             16  sun.reflect.generics.tree.CharSignature
2429:             1             16  sun.reflect.generics.tree.IntSignature
2430:             1             16  sun.reflect.generics.tree.VoidDescriptor
2431:             1             16  sun.security.provider.NativePRNG
2432:             1             16  sun.security.util.AlgorithmDecomposer
2433:             1             16  sun.security.util.DisabledAlgorithmConstraints$Constraints
2434:             1             16  sun.util.calendar.Gregorian
2435:             1             16  sun.util.locale.provider.AuxLocaleProviderAdapter$NullProvider
2436:             1             16  sun.util.locale.provider.CalendarDataUtility$CalendarWeekParameterGetter
2437:             1             16  sun.util.locale.provider.SPILocaleProviderAdapter
2438:             1             16  sun.util.locale.provider.TimeZoneNameUtility$TimeZoneNameGetter
2439:             1             16  sun.util.resources.LocaleData
2440:             1             16  sun.util.resources.LocaleData$LocaleDataResourceBundleControl
Total        257317       15606072

jmap -histo:live <pid>

jmapでは-histoオプションでヒストグラムを取得します。
liveオプションで生存中のオブジェクトのみカウントされ、jcmdでのヒストグラム取得結果と同じになります。

$ jmap -histo:live 75019

 num     #instances         #bytes  class name
----------------------------------------------
   1:         39952        6838208  [C
   2:         16004        1408352  java.lang.reflect.Method
   3:         38902         933648  java.lang.String
   4:          6166         687952  java.lang.Class
   5:         16509         528288  java.util.concurrent.ConcurrentHashMap$Node
   6:          8504         340160  java.util.LinkedHashMap$Entry
   7:          5642         306936  [Ljava.lang.Object;
   8:         13221         289608  [Ljava.lang.Class;
   9:          3563         268144  [Ljava.util.HashMap$Node;
  10:          8376         268032  java.util.HashMap$Node
  11:          4161         233016  java.util.LinkedHashMap
  12:          1198         221696  [B
  13:          2346         168912  java.lang.reflect.Field
  14:           125         164288  [Ljava.util.concurrent.ConcurrentHashMap$Node;
  15:          9511         152176  java.lang.Object
  16:          1807         115384  [Ljava.lang.reflect.Method;
  17:          1251         100080  java.lang.reflect.Constructor
  18:          2449          95224  [I
  19:          2052          93800  [Ljava.lang.String;
  20:          3669          88056  org.springframework.core.MethodClassKey
  21:          1935          77400  java.lang.ref.SoftReference
  22:           781          74976  org.springframework.beans.GenericTypeAwarePropertyDescriptor
  23:          1233          69048  java.lang.Class$ReflectionData
  24:          1854          59328  java.util.LinkedList
  25:          1846          59072  java.lang.ref.WeakReference
  26:          1230          59040  java.util.HashMap
  27:          2191          52584  java.util.ArrayList
  28:          1201          48040  java.util.TreeMap$Entry
  29:          1980          47520  java.beans.MethodRef
  30:           894          42912  org.apache.tomcat.util.modeler.AttributeInfo
        :
        :
        :
        :
2420:             1             16  sun.reflect.GeneratedMethodAccessor9
2421:             1             16  sun.reflect.GeneratedSerializationConstructorAccessor1
2422:             1             16  sun.reflect.GeneratedSerializationConstructorAccessor2
2423:             1             16  sun.reflect.GeneratedSerializationConstructorAccessor3
2424:             1             16  sun.reflect.ReflectionFactory
2425:             1             16  sun.reflect.generics.tree.BooleanSignature
2426:             1             16  sun.reflect.generics.tree.BottomSignature
2427:             1             16  sun.reflect.generics.tree.ByteSignature
2428:             1             16  sun.reflect.generics.tree.CharSignature
2429:             1             16  sun.reflect.generics.tree.IntSignature
2430:             1             16  sun.reflect.generics.tree.VoidDescriptor
2431:             1             16  sun.security.provider.NativePRNG
2432:             1             16  sun.security.util.AlgorithmDecomposer
2433:             1             16  sun.security.util.DisabledAlgorithmConstraints$Constraints
2434:             1             16  sun.util.calendar.Gregorian
2435:             1             16  sun.util.locale.provider.AuxLocaleProviderAdapter$NullProvider
2436:             1             16  sun.util.locale.provider.CalendarDataUtility$CalendarWeekParameterGetter
2437:             1             16  sun.util.locale.provider.SPILocaleProviderAdapter
2438:             1             16  sun.util.locale.provider.TimeZoneNameUtility$TimeZoneNameGetter
2439:             1             16  sun.util.resources.LocaleData
2440:             1             16  sun.util.resources.LocaleData$LocaleDataResourceBundleControl
Total        257317       15606072
ヒープダンプ取得

jcmd <pid> GC.heap_dump <dump_file_name>

GC.heap_dumpで対象プロセスのヒープダンプ取得

$ jcmd 75019 GC.heap_dump jcmd_heap_dump.hprof
75019:
Heap dump file created

ファイル名だけ指定すると出力場所はプロジェクト直下になってしまった。

$ find ./ -name 'jcmd_heap_dump.hprof'
./Desktop/my_git/spring_examples/transaction-propagation-example/jcmd_heap_dump.hprof

ファイル名をフルパスで指定する必要がある。
カレントディレクトリを指定。

$ jcmd 75019 GC.heap_dump /Users/abcdefg/jcmd_heap_dump.hprof
75019:
Heap dump file created

カレントディレクトリに出力された。

$ find ./ -name 'jcmd_heap_dump.hprof'
./jcmd_heap_dump.hprof

jmap -dump:live,format=b,file=<dump_file_name> <pid>

jmapでは-dumpオプションでヒープダンプが取得出来る。
format=bオプションでバイナリで出力され、liveオプションで生存中のオブジェクトのみカウントされる。

$ jmap -dump:live,format=b,file=jmap_heap_dump.hprof 75019

Dumping heap to /Users/abcdefg/jmap_heap_dump.hprof ...
Heap dump file created

出力場所はカレントディレクト

$ find ./ -name 'jmap_heap_dump.hprof'
./jmap_heap_dump.hprof



ヤマダマンさんにliveオプションについて教えてもらったので、修正しました。


終わり。