首页 > 学院 > 开发设计 > 正文

java自带命令工具

2019-11-15 01:04:42
字体:
来源:转载
供稿:网友
java自带命令工具

http://s1.VeVb.com/20150728/yqh0fyyun2z48.jpg

jstat,这个工具很强大,可以监测Java虚拟机GC多方面的状态,具体参数含义参见此链接:

  1. ./jstat-gc8401210003
  2. S0CS1CS0US1UECEUOCOUPCPUYGCYGCTFGCFGCTGCT
  3. 2112.02112.00.00.017024.00.063872.01319.921248.04728.1743416209.64650.046209.692
  4. 2112.02112.00.00.017024.00.063872.01319.921248.04728.1743849209.75550.046209.801
  5. 2112.02112.00.00.017024.00.063872.01319.

jmap,这大概是最常用的命令。下面这个命令不但可以列出类的实例数量,还有强制进行一次full GC的“副作用”,这样的副作用对于定位某些问题很有帮助,参见《使用堆外内存》:

  1. ./jmap-histo:live84012
  2. num#instances#bytesclassname
  3. ----------------------------------------------
  4. 1:8241177656[B
  5. 2:80961106672
  6. ...

使用-heap参数,则可以打印堆的使用情况:

  1. ./jmap-heap84012
  2. AttachingtoPRocessID84012,pleasewait...
  3. Debuggerattachedsuccessfully.
  4. Servercompilerdetected.
  5. JVMversionis20.65-b04-462
  6. usingparallelthreadsinthenewgeneration.
  7. usingthread-localobjectallocation.
  8. ConcurrentMark-SweepGC
  9. HeapConfiguration:
  10. MinHeapFreeRatio=40
  11. MaxHeapFreeRatio=70
  12. MaxHeapSize=132120576(126.0MB)
  13. NewSize=21757952(20.75MB)
  14. MaxNewSize=174456832(166.375MB)
  15. OldSize=65404928(62.375MB)
  16. NewRatio=7
  17. SurvivorRatio=8
  18. PermSize=21757952(20.75MB)
  19. MaxPermSize=85983232(82.0MB)
  20. HeapUsage:
  21. NewGeneration(Eden+1SurvivorSpace):
  22. capacity=19595264(18.6875MB)
  23. used=16785688(16.008079528808594MB)
  24. free=2809576(2.6794204711914062MB)
  25. 85.66196403375837%used
  26. EdenSpace:
  27. capacity=17432576(16.625MB)
  28. used=16785688(16.008079528808594MB)
  29. free=646888(0.6169204711914062MB)
  30. 96.28920017328477%used
  31. FromSpace:
  32. capacity=2162688(2.0625MB)
  33. used=0(0.0MB)
  34. free=2162688(2.0625MB)
  35. 0.0%used
  36. ToSpace:
  37. capacity=2162688(2.0625MB)
  38. used=0(0.0MB)
  39. free=2162688(2.0625MB)
  40. 0.0%used
  41. concurrentmark-sweepgeneration:
  42. capacity=65404928(62.375MB)
  43. used=1390576(1.3261566162109375MB)
  44. free=64014352(61.04884338378906MB)
  45. 2.126102791520541%used
  46. PermGeneration:
  47. capacity=21757952(20.75MB)
  48. used=4852336(4.6275482177734375MB)
  49. free=16905616(16.122451782226562MB)
  50. 22.301437194088855%used

使用-permstat参数,查看永久区:

  1. ./jmap-permstat84012
  2. AttachingtoprocessID84012,pleasewait...
  3. Debuggerattachedsuccessfully.
  4. Servercompilerdetected.
  5. JVMversionis20.65-b04-462
  6. 1239internStringsoccupying104312bytes.
  7. findingclassloaderinstances..Warning:skippinginvalidTLABforthreadt@59779
  8. Warning:skippinginvalidTLABforthreadt@59527
  9. Warning:skippinginvalidTLABforthreadt@59907
  10. Warning:skippinginvalidTLABforthreadt@60163
  11. Warning:skippinginvalidTLABforthreadt@60419
  12. Warning:skippinginvalidTLABforthreadt@60675
  13. FindingobjectsizeusingPrintezisbitsandskippingover...
  14. done.
  15. computingperloaderstat..done.
  16. pleasewait..computingliveness...done.
  17. class_loaderclassesbytesparent_loaderalive?type
  18. 5903973048nulllive
  19. 0x00000007f44cace000nulllivesun/misc/Launcher$ExtClassLoader@0x00000007faff8a40
  20. 0x00000007f44c147881439280x00000007f44cace0livesun/misc/Launcher$AppClassLoader@0x00000007fb056e88
  21. total=35984116976N/Aalive=3,dead=0N/A

把内存中的堆dump成一个镜像文件:

  1. ./jmap-dump:live,format=b,file=/Users/xiongyi/Documents/dump.core84012

jstack,线程堆栈打印。注意waiting to lock <xxx>在等待锁,比如进入临界区时;locked <xxx>表示当前同步操作,线程锁住了某资源;而waiting on <xxx>指的是在同步块内,wait方法的执行中暂时地释放了该锁的占用,等唤醒的时候需要重新获取:

  1. ./jstack84012
  2. 2013-11-1118:30:35
  3. FullthreaddumpJavaHotSpot(TM)64-BitServerVM(20.65-b04-462mixedmode):
  4. "AttachListener"daemonprio=9tid=7ff64e206800nid=0x117782000waitingoncondition[00000000]
  5. java.lang.Thread.State:RUNNABLE
  6. "LowMemoryDetector"daemonprio=5tid=7ff64c80f000nid=0x117c96000runnable[00000000]
  7. java.lang.Thread.State:RUNNABLE
  8. "C2CompilerThread1"daemonprio=9tid=7ff64c80e800nid=0x117b93000waitingoncondition[00000000]
  9. java.lang.Thread.State:RUNNABLE
  10. "C2CompilerThread0"daemonprio=9tid=7ff64c80d800nid=0x117a90000waitingoncondition[00000000]
  11. java.lang.Thread.State:RUNNABLE
  12. "SignalDispatcher"daemonprio=9tid=7ff64c80d000nid=0x11798d000runnable[00000000]
  13. java.lang.Thread.State:RUNNABLE
  14. "SurrogateLockerThread(ConcurrentGC)"daemonprio=5tid=7ff64c80c000nid=0x11788a000waitingoncondition[00000000]
  15. java.lang.Thread.State:RUNNABLE
  16. "Finalizer"daemonprio=8tid=7ff64e13d800nid=0x11767f000inObject.wait()[11767e000]
  17. java.lang.Thread.State:WAITING(onobjectmonitor)
  18. atjava.lang.Object.wait(NativeMethod)
  19. -waitingon<7f44c0ed0>(ajava.lang.ref.ReferenceQueue$Lock)
  20. atjava.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
  21. -locked<7f44c0ed0>(ajava.lang.ref.ReferenceQueue$Lock)
  22. atjava.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)
  23. atjava.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:171)
  24. "ReferenceHandler"daemonprio=10tid=7ff64e13c800nid=0x11757c000inObject.wait()[11757b000]
  25. java.lang.Thread.State:WAITING(onobjectmonitor)
  26. atjava.lang.Object.wait(NativeMethod)
  27. -waitingon<7f44c0018>(ajava.lang.ref.Reference$Lock)
  28. atjava.lang.Object.wait(Object.java:485)
  29. atjava.lang.ref.Reference$ReferenceHandler.run(Reference.java:116)
  30. -locked<7f44c0018>(ajava.lang.ref.Reference$Lock)
  31. "main"prio=5tid=7ff64c800800nid=0x10f709000runnable[10f708000]
  32. java.lang.Thread.State:RUNNABLE
  33. attest.Test.main(Test.java:7)
  34. "VMThread"prio=9tid=7ff64e138000nid=0x117479000runnable
  35. "Gangworker#0(ParallelGCThreads)"prio=9tid=7ff64e000000nid=0x112b0f000runnable
  36. "Gangworker#1(ParallelGCThreads)"prio=9tid=7ff64e001000nid=0x112c12000runnable
  37. "Gangworker#2(ParallelGCThreads)"prio=9tid=7ff64e001800nid=0x112d15000runnable
  38. "Gangworker#3(ParallelGCThreads)"prio=9tid=7ff64e002000nid=0x112e18000runnable
  39. "Gangworker#4(ParallelGCThreads)"prio=9tid=7ff64e002800nid=0x112f1b000runnable
  40. "Gangworker#5(ParallelGCThreads)"prio=9tid=7ff64e003800nid=0x11301e000runnable
  41. "Gangworker#6(ParallelGCThreads)"prio=9tid=7ff64e004000nid=0x113121000runnable
  42. "Gangworker#7(ParallelGCThreads)"prio=9tid=7ff64e004800nid=0x113224000runnable
  43. "ConcurrentMark-SweepGCThread"prio=9tid=7ff64e0e2000nid=0x1170f0000runnable
  44. "Gangworker#0(ParallelCMSThreads)"prio=9tid=7ff64e0e0800nid=0x1166ea000runnable
  45. "Gangworker#1(ParallelCMSThreads)"prio=9tid=7ff64e0e1800nid=0x1167ed000runnable
  46. "VMPeriodicTaskThread"prio=10tid=7ff64c820800nid=0x117d99000waitingoncondition
  47. "ExceptionCatcherThread"prio=10tid=7ff64c801800nid=0x10f936000runnable
  48. JNIglobalreferences:963

jinfo,可以打印JVM执行的参数信息,有一个非常大的作用在于,部分JVM参数在执行过程中是可以修改的,请参见这篇《通过jinfo工具在full GC前后做heap dump》,通过jinfo改变参数HeapDumpBeforeFullGC和HeapDumpAfterFullGC,输出heap dump后,再改回来。

jhat,可以比对core文件之间的对象变化,支持对象查询语言(OQL),请参见这里。

  1. ./jhat-stacktrue-refstrue-port8080-baseline/xxx/dump-baseline.core-debug1/Users/xiongyi/Documents/dump-newer.core

javap,用于反编译class文件,对于JVM指令集,这里有完整的文档。

  1. javap-c-v./Test.class
  2. Classfilexxx/Test.class
  3. LastmodifiedNov11,2013;size441bytes
  4. md5checksum69488187cc8a8f166bc6dd0d517fb4cb
  5. Compiledfrom"Test.java"
  6. publicclasstest.Test
  7. SourceFile:"Test.java"
  8. minorversion:0
  9. majorversion:50
  10. flags:ACC_PUBLIC,ACC_SUPER
  11. Constantpool:
  12. #1=Methodref#6.#16//java/lang/Object."":()V
  13. #2=Integer1048576
  14. #3=Fieldref#17.#18//java/lang/System.out:Ljava/io/PrintStream;
  15. #4=Methodref#19.#20//java/io/PrintStream.println:(Ljava/lang/Object;)V
  16. #5=Class#21//test/Test
  17. #6=Class#22//java/lang/Object
  18. #7=Utf8
  19. #8=Utf8()V
  20. #9=Utf8Code
  21. #10=Utf8LineNumberTable
  22. #11=Utf8main
  23. #12=Utf8([Ljava/lang/String;)V
  24. #13=Utf8StackMapTable
  25. #14=Utf8SourceFile
  26. #15=Utf8Test.java
  27. #16=NameAndType#7:#8//"":()V
  28. #17=Class#23//java/lang/System
  29. #18=NameAndType#24:#25//out:Ljava/io/PrintStream;
  30. #19=Class#26//java/io/PrintStream
  31. #20=NameAndType#27:#28//println:(Ljava/lang/Object;)V
  32. #21=Utf8test/Test
  33. #22=Utf8java/lang/Object
  34. #23=Utf8java/lang/System
  35. #24=Utf8out
  36. #25=Utf8Ljava/io/PrintStream;
  37. #26=Utf8java/io/PrintStream
  38. #27=Utf8println
  39. #28=Utf8(Ljava/lang/Object;)V
  40. {
  41. publictest.Test();
  42. flags:ACC_PUBLIC
  43. Code:
  44. stack=1,locals=1,args_size=1
  45. 0:aload_0
  46. 1:invokespecial#1//Methodjava/lang/Object."":()V
  47. 4:return
  48. LineNumberTable:
  49. line4:0
  50. publicstaticvoidmain(java.lang.String[]);
  51. flags:ACC_PUBLIC,ACC_STATIC
  52. Code:
  53. stack=2,locals=2,args_size=1
  54. 0:ldc#2//int1048576
  55. 2:newarraybyte
  56. 4:astore_1
  57. 5:getstatic#3//Fieldjava/lang/System.out:Ljava/io/PrintStream;
  58. 8:aload_1
  59. 9:invokevirtual#4//Methodjava/io/PrintStream.println:(Ljava/lang/Object;)V
  60. 12:goto0
  61. LineNumberTable:
  62. line7:0
  63. line8:5
  64. line9:12
  65. StackMapTable:number_of_entries=1
  66. frame_type=0/*same*/
  67. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表