java自带的jvm分析工具

这段时间觉得很有必要对java的内存分析工具进行熟悉,这样以后出现机器负载较高,或者反应很慢的时候,我就可以查找原因了。上网搜了搜,发现下面这些是比较常用的,然后我在机器上试试了,把结果也贴出来哈。

1.jps
类似ps -ef|grep java 显示java进程号

或者pgrep java
2.jstack
打印jvm内存的堆栈信息,打印出来的结果类似
2010-04-21 20:10:51
Full thread dump Java HotSpot(TM) Server VM (10.0-b23 mixed mode):

“RMI TCP Connection(idle)” daemon prio=10 tid=0x08f7a000 nid=0x1928 waiting on condition [0x4b234000..0x4b2350a0]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
– parking to wait for <0x68ec3430> (a java.util.concurrent.SynchronousQueue$TransferStack)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:198)
at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:424)
at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:323)
at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:874)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:944)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:906)
at java.lang.Thread.run(Thread.java:619)

“Attach Listener” daemon prio=10 tid=0x08a08800 nid=0x18e4 runnable [0x00000000..0x4b142068]
java.lang.Thread.State: RUNNABLE

“recvMsgTP-1_sharereport_groupId_refund_[daily]-33040763-3-thread-10” prio=10 tid=0x08f3d400 nid=0x2985 waiting on condition [0x4b192000..0x4b192fa0]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
– parking to wait for <0x6af53cd8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1925)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:358)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:946)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:906)
at java.lang.Thread.run(Thread.java:619)

这样我们可以根据打印结果,看到现在哪些线程在运行,哪些在等待, 进而再找到线程等待执行的原因,从而分析出程序执行变慢的原因。

3.jstat -gcutil
对java 垃圾回收信息的统计,这样我们可以得到垃圾回收是否正常,full GC的执行时间和频率是否正常等。
S0 S1 E O P YGC YGCT FGC FGCT GCT
8.34 0.00 53.24 13.14 51.77 74 7.596 28 25.099 32.695
8.34 0.00 53.54 13.14 51.77 74 7.596 28 25.099 32.695
8.34 0.00 53.93 13.14 51.77 74 7.596 28 25.099 32.695
8.34 0.00 53.93 13.14 51.77 74 7.596 28 25.099 32.695
8.34 0.00 53.93 13.14 51.77 74 7.596 28 25.099 32.695
8.34 0.00 53.97 13.14 51.77 74 7.596 28 25.099 32.695
8.34 0.00 53.98 13.14 51.77 74 7.596 28 25.099 32.695
8.34 0.00 53.98 13.14 51.77 74 7.596 28 25.099 32.695
8.34 0.00 53.98 13.14 51.77 74 7.596 28 25.099 32.695
8.34 0.00 54.01 13.14 51.77 74 7.596 28 25.099 32.695
8.34 0.00 54.40 13.14 51.77 74 7.596 28 25.099 32.695

后面加T表示的是Time 执行时间,单位是秒
YGC FGC 分别是young GC和Full GC执行的次数。

4.jinfo – flags
可以查询java运行的参数设置
Attaching to process ID 21982, please wait…
Debugger attached successfully.
Server compiler detected.
JVM version is 10.0-b23

-Dprogram.name=run.sh -Xms1024m -Xmx1024m -XX:NewSize=320m -XX:MaxNewSize=320m -XX:PermSize=96m -XX:MaxPermSize=96m -Djava.awt.headless=true -Dsun.net.client.defaultConnectTimeout=10000 -Dsun.net.client.defaultReadTimeout=30000 -Xdebug -Xrunjdwp:transport=dt_socket,address=18787,server=y,suspend=n -Djava.net.preferIPv4Stack=true

5.jmap
-histo
打印出java中的对象信息,包括实例个数,字节大小和完整类名。
num #instances #bytes class name
———————————————-
1: 660495 66381608 [C
2: 134119 34894408 [B
3: 73441 30779672 [I
4: 670401 16089624 java.lang.String
5: 125062 15547312 <constMethodKlass>
6: 427296 13673472 java.util.TreeMap$Entry
7: 328585 13143400 java.util.concurrent.ConcurrentHashMap$EntryIterator
8: 125062 10010904 <methodKlass>
9: 132205 8504792 [Ljava.lang.Object;
10: 173806 7883528 <symbolKlass>

这些命令后面都要加上java进程号。