最近在温故《深入理解Java虚拟机》,对第四章的工具进行笔记记录,备忘。
jps
用来查看正在运行的虚拟机进程,功能单一但高频的小工具。使用如下:
hadoop@hzbxs-bigdata16:~$ jps
40759 AzkabanWebServer
41031 AzkabanExecutorServer
11860 Jps
第一列是LVMID(Local Virtual Machine Identifier),本地虚拟机进程的LVMID就是操作系统的进程ID;第二列是进程启动的主类。
jps的常用选项有:
选项 | 用途 |
---|---|
-m | 输出虚拟机进程启动时传递给主类main()函数的参数 |
-v | 输出虚拟机进程启动时JVM参数 |
jstat
用来在虚拟机运行中监视各种运行状态信息,是运行期定位虚拟机性能问题的首选工具。格式如下: jstat -
下边是使用范例,jstat -gc 40759 2000 5 ,表示查询进程40759的Java堆情况,查询5次,间隔2000ms:
hadoop@hzbxs-bigdata16:~$ jstat -gc 40759 2000 5
Warning: Unresolved Symbol: sun.gc.generation.2.space.0.capacity substituted NaN
Warning: Unresolved Symbol: sun.gc.generation.2.space.0.used substituted NaN
S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT
512.0 512.0 0.0 128.0 18432.0 3045.8 149504.0 90560.0 � � 2433 12.128 5 0.475 12.603
512.0 512.0 0.0 128.0 18432.0 3045.8 149504.0 90560.0 � � 2433 12.128 5 0.475 12.603
512.0 512.0 0.0 128.0 18432.0 3045.8 149504.0 90560.0 � � 2433 12.128 5 0.475 12.603
512.0 512.0 0.0 128.0 18432.0 3045.8 149504.0 90560.0 � � 2433 12.128 5 0.475 12.603
512.0 512.0 0.0 128.0 18432.0 3050.5 149504.0 90560.0 � � 2433 12.128 5 0.475 12.603
主要选项比较多,出了上边的-gc,下边再介绍 -gcutil,输出的内容和-gc基本相同,但是主要关注空间占用全部空间的总百分比。如下:
hadoop@hzbxs-bigdata16:~$ jstat -gcutil 40759 2000 5
Warning: Unresolved Symbol: sun.gc.generation.2.space.0.capacity substituted NaN
Warning: Unresolved Symbol: sun.gc.generation.2.space.0.used substituted NaN
Warning: Unresolved Symbol: sun.gc.generation.2.space.0.capacity substituted NaN
S0 S1 E O P YGC YGCT FGC FGCT GCT
31.25 0.00 43.46 60.57 � 2428 12.108 5 0.475 12.582
31.25 0.00 43.46 60.57 � 2428 12.108 5 0.475 12.582
31.25 0.00 43.46 60.57 � 2428 12.108 5 0.475 12.582
31.25 0.00 43.46 60.57 � 2428 12.108 5 0.475 12.582
31.25 0.00 43.46 60.57 � 2428 12.108 5 0.475 12.582
各区含义详见书的前几章:S0 S1 代表两个Survivor区,E代表Eden区(新生代),O代表Old区(老年代),P代表(永久代),YGC(Minor GC,Young GC发生次数),YGCT(耗时),FGC(Full GC发生次数),FGCT(耗时),GCT(GC 总耗时)
jinfo
用于是实时查看和修改虚拟机各项参数。感觉不好用,简单Mark算了:
hadoop@hzbxs-bigdata16:~$ jinfo -flag CMSInitiatingOccupancyFraction 40759
-XX:CMSInitiatingOccupancyFraction=-1
jmap
用于生成堆转储快照(也就是Heapdump文件)
下边仅介绍常用的-dump参数:
hadoop@hzbxs-bigdata16:~$ jmap -dump:file=mydump.bin,format=b 40759
Dumping heap to /home/hadoop/mydump.bin ...
Heap dump file created
hadoop@hzbxs-bigdata16:~$ ls mydump.bin
mydump.bin
导出bin文件,用 内存分析工具打开,可以看到非常详尽的信息。工具可以去eclipse官网下载(Eclipse Memory Analyzer),可以下载插件模式或者是独立软件模式,建议后者。
jstack
用于生成虚拟机当前时刻的线程堆栈信息。这个工具是定位线程死锁、死循环等等各种卡顿问题的利器。使用非常简单,如下:
hadoop@hzbxs-bigdata16:~$ jstack 21024 >> mystack.log
将jstack的输出重定向到我的文件mystack.log,然后就随意看,less、vim、notepad任君选择。如下:
2017-06-18 16:17:42
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.72-b15 mixed mode):
"Attach Listener" #96 daemon prio=9 os_prio=0 tid=0x0000000000f96000 nid=0xd25b waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"257026550@qtp-1033304734-10" #90 prio=5 os_prio=0 tid=0x00007fbfba139800 nid=0x8624 in Object.wait() [0x00007fbfb729a000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:626)
- locked <0x00000006c7ce6b80> (a org.mortbay.thread.QueuedThreadPool$PoolThread)
"pool-1-thread-5" #43 prio=5 os_prio=0 tid=0x00007fbfb9d94800 nid=0x5d0a waiting on condition [0x00007fbfb6591000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000006c01cdcc0> (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.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
除了以上几个常用的命令工具,还有两个非常强大的可视化工具,分别是JConsole 和 VisualVM. 这两个工具很强大很简单,有时间再学。