当前位置: 代码迷 >> JavaScript >> JVM有关问题诊断常用命令:jinfo,j地图,jstack
  详细解决方案

JVM有关问题诊断常用命令:jinfo,j地图,jstack

热度:663   发布时间:2012-11-01 11:11:33.0
JVM问题诊断常用命令:jinfo,jmap,jstack

转载自 ---- http://singleant.iteye.com/blog/739476

?

1.jinfo

描述:输出给定 java 进程所有的配置信息。包括 java 系统属性和 jvm 命令行标记等。

用法:

jinfo [ option ] pid

jinfo [ option ] executable core

jinfo [ option ] [server-id@]remote-hostname-or-IP

例子:

jinfo pid

得到结果如下:

Java代码 ?收藏代码
  1. JVM?version?is? 14.0 -b16??
  2. ??
  3. Java?System?Properties:??
  4. ??
  5. java.runtime.name?=?Java(TM)?SE?Runtime?Environment??
  6. ??
  7. sun.boot.library.path?=?/work/ProgramFiles/jdk1.6 .0_14/jre/lib/i386??
  8. ??
  9. java.vm.version?=?14.0 -b16??
  10. ??
  11. java.vm.vendor?=?Sun?Microsystems?Inc.??
  12. ??
  13. java.vendor.url?=?http://java.sun.com/ ??
  14. ??
  15. path.separator?=?:??
  16. ??
  17. java.vm.name?=?Java?HotSpot(TM)?Server?VM??
  18. ??
  19. file.encoding.pkg?=?sun.io??
  20. ??
  21. sun.java.launcher?=?SUN_STANDARD??
  22. ??
  23. user.country?=?CN??
  24. ??
  25. sun.os.patch.level?=?unknown??
  26. ??
  27. java.vm.specification.name?=?Java?Virtual?Machine?Specification??
  28. ??
  29. user.dir?=?/work/RateWorkspace/branches/rate/daemon??
  30. ??
  31. java.runtime.version?=?1.6 .0_14-b08??
  32. ??
  33. java.awt.graphicsenv?=?sun.awt.X11GraphicsEnvironment??
  34. ??
  35. java.endorsed.dirs?=?/work/ProgramFiles/jdk1.6 .0_14/jre/lib/endorsed??
  36. ??
  37. os.arch?=?i386??
  38. ??
  39. java.io.tmpdir?=?/tmp??
  40. ??
  41. line.separator?=???
  42. ??
  43. ??
  44. ??
  45. java.vm.specification.vendor?=?Sun?Microsystems?Inc.??
  46. ??
  47. os.name?=?Linux??
  48. ??
  49. sun.jnu.encoding?=?UTF-8 ??
  50. ??
  51. java.library.path?=?/work/ProgramFiles/jdk1.6 .0_14/jre/lib/i386/server:/work/ProgramFiles/jdk1. 6 .0_14/jre/lib/i386:/work/ProgramFiles/jdk1. 6 .0_14/jre/../lib/i386:/usr/lib/jvm/java- 6 -openjdk/jre/lib/i386/client:/usr/lib/jvm/java- 6 -openjdk/jre/lib/i386:/usr/lib/xulrunner-addons:/usr/lib/xulrunner-addons:/usr/java/packages/lib/i386:/lib:/usr/lib??
  52. ??
  53. java.specification.name?=?Java?Platform?API?Specification??
  54. ??
  55. java.class .version?=? 50.0 ??
  56. ??
  57. sun.management.compiler?=?HotSpot?Tiered?Compilers??
  58. ??
  59. os.version?=?2.6 . 28 - 19 -generic??
  60. ??
  61. user.home?=?/home/yblin??
  62. ??
  63. user.timezone?=?PRC??
  64. ??
  65. java.awt.printerjob?=?sun.print.PSPrinterJob??
  66. ??
  67. file.encoding?=?GBK??
  68. ??
  69. java.specification.version?=?1.6 ??
  70. ??
  71. java.class .path?=???
  72. ??
  73. user.name?=?yblin??
  74. ??
  75. java.vm.specification.version?=?1.0 ??
  76. ??
  77. java.home?=?/work/ProgramFiles/jdk1.6 .0_14/jre??
  78. ??
  79. sun.arch.data.model?=?32 ??
  80. ??
  81. user.language?=?zh??
  82. ??
  83. java.specification.vendor?=?Sun?Microsystems?Inc.??
  84. ??
  85. java.vm.info?=?mixed?mode??
  86. ??
  87. java.version?=?1.6 .0_14??
  88. ??
  89. java.ext.dirs?=?/work/ProgramFiles/jdk1.6 .0_14/jre/lib/ext:/usr/java/packages/lib/ext??
  90. ??
  91. sun.boot.class .path?=?/work/ProgramFiles/jdk1. 6 .0_14/jre/lib/resources.jar:/work/ProgramFiles/jdk1. 6 .0_14/jre/lib/rt.jar:/work/ProgramFiles/jdk1. 6 .0_14/jre/lib/sunrsasign.jar:/work/ProgramFiles/jdk1. 6 .0_14/jre/lib/jsse.jar:/work/ProgramFiles/jdk1. 6 .0_14/jre/lib/jce.jar:/work/ProgramFiles/jdk1. 6 .0_14/jre/lib/charsets.jar:/work/ProgramFiles/jdk1. 6 .0_14/jre/classes??
  92. ??
  93. java.vendor?=?Sun?Microsystems?Inc.??
  94. ??
  95. file.separator?=?/??
  96. ??
  97. java.vendor.url.bug?=?http://java.sun.com/cgi-bin/bugreport.cgi ??
  98. ??
  99. sun.io.unicode.encoding?=?UnicodeLittle??
  100. ??
  101. sun.cpu.endian?=?little??
  102. ??
  103. sun.desktop?=?gnome??
  104. ??
  105. sun.cpu.isalist?=???
  106. ??
  107. ??
  108. VM?Flags:??
  109. ??
  110. -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:55948 ?-ea?-Dfile.encoding=GBK??

?

?

?

?

这个命令包含了 JDK JVM 运行起来时的一些属性。

?

2.jmap

描述 :输出给定 java 进程所有的内存使用信息。

使用:

??? jmap [ option ] pid

??? jmap [ option ] executable core

??? jmap [ option ] [server-id@]remote-hostname-or-IP

?

?

<no option>

没有可选项时,会输出所有共享的对象映射。

开始地址,映射大小,全路径。

直接 jmap pid得到如下结果:

0x08048000???? 46K? /work/ProgramFiles/jdk1.6.0_14/bin/java

0xb6763000???? 74K? /work/ProgramFiles/jdk1.6.0_14/jre/lib/i386/libzip.so

0xb6774000???? 41K? /lib/tls/i686/cmov/libnss_files-2.9.so

0xb6780000???? 37K? /lib/tls/i686/cmov/libnss_nis-2.9.so

0xb678b000???? 29K? /lib/tls/i686/cmov/libnss_compat-2.9.so

目前还说不是很理解这个执行结果是什么意思。经询问同事了解,是加载的文件的信息。

?

-heap 选项

jmap -heap pid : 查看堆的配置和大当前各个区块的使用情况,如伊甸区、老年代,永久代。可以用此来判断内存目前的使用情况以及垃圾回收情况:

jmap -heap pid,得到如下结果:

?

Java代码 ?收藏代码
  1. <strong>Heap?Configuration:??
  2. ??
  3. ???MinHeapFreeRatio?=?40 ??
  4. ??
  5. ???MaxHeapFreeRatio?=?70 ??
  6. ??
  7. ???MaxHeapSize??????=?859832320 ?( 820 .0MB)??
  8. ??
  9. ???NewSize??????????=?1048576 ?( 1 .0MB)??
  10. ??
  11. ???MaxNewSize???????=?4294901760 ?( 4095 .9375MB)??
  12. ??
  13. ???OldSize??????????=?4194304 ?( 4 .0MB)??
  14. ??
  15. ???NewRatio?????????=?8 ??
  16. ??
  17. ???SurvivorRatio????=?8 ??
  18. ??
  19. ???PermSize?????????=?16777216 ?( 16 .0MB)??
  20. ??
  21. ???MaxPermSize??????=?67108864 ?( 64 .0MB)??
  22. ??
  23. ??
  24. Heap?Usage:??
  25. ??
  26. PS?Young?Generation??
  27. ??
  28. Eden?Space:??
  29. ??
  30. ???capacity?=?9043968 ?( 8 .625MB)??
  31. ??
  32. ???used?????=?6820384 ?( 6 .504425048828125MB)??
  33. ??
  34. ???free?????=?2223584 ?( 2 .120574951171875MB)??
  35. ??
  36. ???75.41362375452898 %?used??
  37. ??
  38. From?Space:??
  39. ??
  40. ???capacity?=?720896 ?( 0 .6875MB)??
  41. ??
  42. ???used?????=?704680 ?( 0 .6720352172851562MB)??
  43. ??
  44. ???free?????=?16216 ?( 0 .01546478271484375MB)??
  45. ??
  46. ???97.7505770596591 %?used??
  47. ??
  48. To?Space:??
  49. ??
  50. ???capacity?=?720896 ?( 0 .6875MB)??
  51. ??
  52. ???used?????=?0 ?( 0 .0MB)??
  53. ??
  54. ???free?????=?720896 ?( 0 .6875MB)??
  55. ??
  56. ???0.0 %?used??
  57. ??
  58. PS?Old?Generation??
  59. ??
  60. ???capacity?=?47710208 ?( 45 .5MB)??
  61. ??
  62. ???used?????=?589184 ?( 0 .5618896484375MB)??
  63. ??
  64. ???free?????=?47121024 ?( 44 .9381103515625MB)??
  65. ??
  66. ???1.2349223042582418 %?used??
  67. ??
  68. PS?Perm?Generation??
  69. ??
  70. ???capacity?=?16777216 ?( 16 .0MB)??
  71. ??
  72. ???used?????=?5179648 ?( 4 .939697265625MB)??
  73. ??
  74. ???free?????=?11597568 ?( 11 .060302734375MB)??
  75. ??
  76. ???30.87310791015625 %?used??
  77. ??
  78. </strong>??


?

?

?

?

-histo 选项

内存使用的柱状图,包含每个类,对象使用的内存大小。 VM 内部的类名以 * 前缀开头。

Java代码 ?收藏代码
  1. num?????#instances?????????#bytes?? class ?name??
  2. ??
  3. ---------------------------------------------??
  4. ??
  5. ??1 :????????? 57195 ???????? 5639792 ??[C??
  6. ??
  7. ??2 :????????? 34680 ???????? 3991104 ??<constMethodKlass>??
  8. ??
  9. ??3 :????????? 34680 ???????? 2779112 ??<methodKlass>??
  10. ??
  11. ??4 :????????? 52947 ???????? 2340272 ??<symbolKlass>??
  12. ??
  13. ??5 :?????????? 3024 ???????? 1641744 ??<constantPoolKlass>??
  14. ??
  15. ??6 :?????????? 5182 ???????? 1618040 ??[B??
  16. ??
  17. ??7 :?????????? 3024 ???????? 1290544 ??<instanceKlassKlass>??
  18. ??
  19. ??8 :????????? 12134 ???????? 1267696 ??[I??
  20. ??
  21. ??9 :????????? 44622 ???????? 1070928 ??java.lang.String??
  22. ??
  23. ?10 :?????????? 2565 ????????? 994128 ??<constantPoolCacheKlass>??
  24. ??
  25. ?………………??

?

?

?

?

-permstat 选项

输出所有类装载器在堆里产生的对象

包括每个装载器的名字,活跃,地址,父装载器,和其总共加载的类大小。

?

jmap -permstat pid

得到如下结果:

Java代码 ?收藏代码
  1. class_loader????classes?????bytes????????parent_loader?alive?????????type??
  2. ??
  3. ??
  4. <bootstrap>?????1102 ????????? 4283352 ???? null ?????live???<internal>??
  5. ??
  6. 0x80753d50 ????? 1 ??????? 1992 ??????????? null ?????dead?????????sun/reflect/DelegatingClassLoader @0x7c2e6c28 ??
  7. ??
  8. 0x8034d9e0 ????? 8 ??????? 32272 ????????? null ?????live???sun/misc/Launcher$ExtClassLoader @0x7c432400 ??
  9. ??
  10. 0x8095a4e0 ????? 1 ??????? 1784 ????????? 0x8034d998 ?????dead?????????sun/reflect/DelegatingClassLoader @0x7c2e6c28 ??
  11. ??
  12. 0xadc25868 ????? 1 ??????? 1784 ????????? 0x8034d998 ?????dead?????????sun/reflect/DelegatingClassLoader @0x7c2e6c28 ??
  13. ??
  14. 0xaebf74c0 ?????? 1 ??????? 1784 ????????? 0x8034d998 ?????dead?????????sun/reflect/DelegatingClassLoader @0x7c2e6c28 ??
  15. ??
  16. 0xadc2b5c8 ?????? 1 ??????? 1784 ????????? 0x8034d998 ?????dead?????????sun/reflect/DelegatingClassLoader @0x7c2e6c28 ??
  17. ??
  18. 0x808cd448 ????? 1 ??????? 1768 ????????? 0x8034d998 ?????dead?????????sun/reflect/DelegatingClassLoader @0x7c2e6c28 ??
  19. ??
  20. 0x80758288 ????? 1 ??????? 1768 ????????? 0x8034d998 ?????dead?????????sun/reflect/DelegatingClassLoader @0x7c2e6c28 ??
  21. ??
  22. 0x80959e80 ????? 1 ??????? 1784 ????????? 0x8034d998 ?????dead?????????sun/reflect/DelegatingClassLoader @0x7c2e6c28 ??
  23. ??
  24. 0x80756248 ????? 1 ??????? 1768 ????????? 0x8034d998 ?????dead?????????sun/reflect/DelegatingClassLoader @0x7c2e6c28 ??
  25. ??
  26. 0x808e6ae8 ????? 1 ??????? 1784 ????????? 0x8034d998 ?????dead?????????sun/reflect/DelegatingClassLoader @0x7c2e6c28 ??
  27. ??
  28. ??
  29. ???

?

?

?

?

?

3.jstack - Stack Trace

为指定的线程输出 java 的线程堆栈信息,包括了进程里的所有线程。每一个线程 frame ,包括类全名,方法名,代码行。

?

java.lang.Thread.State RUNNABLE BLOCKED TIMED_WATTING(sleep 后会进入这种状态 ) (如果是 BLOCKED 状态就要注意了,看看 blocked 的状态在等待什么?因为什么而阻塞?)最常见的情况是线程在等待网络的读写,比如当网络数据没有准备好读时,线程处于这种等待状态,而一旦有数据准备好读之后,线程会重新激活,读取并处理数据。

?

?

在线程中,有一些 JVM 内部的后台线程,来执行譬如垃圾回收,或者低内存的检测等等任务,这些线程往往在 JVM 初始化的时候就存在,如下所示:

"Low Memory Detector" daemon prio=10 tid=0x081465f8 nid=0x7 runnable [0x00000000..0x00000000]

"CompilerThread0" daemon prio=10 tid=0x08143c58 nid=0x6 waiting on condition [0x00000000..0xfb5fd798]

"Signal Dispatcher" daemon prio=10 tid=0x08142f08 nid=0x5 waiting on condition [0x00000000..0x00000000]

"Finalizer" daemon prio=10 tid=0x08137ca0 nid=0x4 in Object.wait() [0xfbeed000..0xfbeeddb8]

?

写了个代码, 100 个线程同时往控制台输出,代码如下:

?

Java代码 ?收藏代码
  1. public ? class ?MyThread? extends ?Thread?{??
  2. ??
  3. ???
  4. ??
  5. ????public ?MyThread(String?name){??
  6. ??
  7. ????????super (name);??
  8. ??
  9. ????}??
  10. ??
  11. ???
  12. ??
  13. ????@Override ??
  14. ??
  15. ????public ? void ?run()?{??
  16. ??
  17. ????????super .run();??
  18. ??
  19. ????????while ?( true )?{??
  20. ??
  21. ????????????try ?{??
  22. ??
  23. ????????????????System.out.println("1" );??
  24. ??
  25. ????????????}?catch ?(Exception?e)?{??
  26. ??
  27. ???
  28. ??
  29. ????????????}??
  30. ??
  31. ????????}??
  32. ??
  33. ???
  34. ??
  35. ????}??
  36. ??
  37. ???
  38. ??
  39. ????public ? static ? void ?main(String[]?args)?{??
  40. ??
  41. ????????for ?( int ?i?=? 0 ;?i?<? 100 ;?i++)?{??
  42. ??
  43. ????????????MyThread?my?=?new ?MyThread( "thrad" ?+?i);??
  44. ??
  45. ????????????my.start();??
  46. ??
  47. ????????}??
  48. ??
  49. ???
  50. ??
  51. ????}??
  52. ??
  53. }??
  54. ??
  55. ???

?

?

?

?

?

通过 jstack pid, 得到如下部分结果:

?

Java代码 ?收藏代码
  1. "thrad76" ?prio= 10 ?tid= 0x096ce400 ?nid= 0x4305 ?waiting? for ?monitor?entry?[ 0x7a280000 ]??
  2. ???java.lang.Thread.State:?BLOCKED?(on?object?monitor)??
  3. ????at?java.io.PrintStream.println(PrintStream.java:755 )??
  4. ????-?waiting?to?lock?<0x805782c0 >?(a?java.io.PrintStream)??
  5. ????at?org.jtester.service.MyThread.run(MyThread.java:15 )??
  6. ??
  7. "thrad75" ?prio= 10 ?tid= 0x096ccc00 ?nid= 0x4304 ?runnable?[ 0x7a2d1000 ]??
  8. ???java.lang.Thread.State:?RUNNABLE??
  9. ????at?java.io.PrintStream.write(PrintStream.java:428 )??
  10. ????-?locked?<0x805782c0 >?(a?java.io.PrintStream)??
  11. ????at?sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202 )??
  12. ????at?sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272 )??
  13. ????at?sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85 )??
  14. ????-?locked?<0x805783c8 >?(a?java.io.OutputStreamWriter)??
  15. ????at?java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168 )??
  16. ????at?java.io.PrintStream.write(PrintStream.java:477 )??
  17. ????-?locked?<0x805782c0 >?(a?java.io.PrintStream)??
  18. ????at?java.io.PrintStream.print(PrintStream.java:619 )??
  19. ????at?java.io.PrintStream.println(PrintStream.java:756 )??
  20. ????-?locked?<0x805782c0 >?(a?java.io.PrintStream)??
  21. ????at?org.jtester.service.MyThread.run(MyThread.java:15 )??
  22. ??
  23. "thrad74" ?prio= 10 ?tid= 0x096cb000 ?nid= 0x4303 ?waiting? for ?monitor?entry?[ 0x7a322000 ]??
  24. ???java.lang.Thread.State:?BLOCKED?(on?object?monitor)??
  25. ????at?java.io.PrintStream.println(PrintStream.java:755 )??
  26. ????-?waiting?to?lock?<0x805782c0 >?(a?java.io.PrintStream)??
  27. ????at?org.jtester.service.MyThread.run(MyThread.java:15 )??
?

?

?

可以看到只有线程 thrad75 RUNNABLE 状态的,其他的都是 BLOCKED 状态。因为其他的线程在竞争控制台输出的时候都被阻塞了。