常用工具介绍:jinfo jmap jstack jstat
- 可以从一个给定的java进程或core文件或远程debug服务器上获取java配置信息。包括java系统属性及JVM参数(command line flags)。注意在jvm启动参数中没有配置的参数也可使用jinfo –flag xxx pid输出默认值(很有用,但貌似一些简写的参数查不出来)。
- 可以修改运行时的java 进程的opts。
- 只有solaris和linux的JDK版本里有。
- 使用方式可使用jinfo –h 查询。
观察运行中的jvm物理内存的占用情况。
如果连用SHELL jmap -histo pid>a.log可以将其保存到文本中去,在一段时间后,使用文本对比工具,可以对比出GC回收了哪些对象。
参数很简单,直接查看jmap -h
举例:
jmap -heap pid
jmap -dump:format=b,file=heap.hprof <pid>
观察jvm中当前所有线程的运行情况和线程当前状态
如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程序何处发生问题。另外,jstack工具还可以附属到正在运行的java程序中,看到当时运行的java程序的java stack和native stack的信息, 如果现在运行的java程序呈现hung的状态,jstack是非常有用的。目前只有在Solaris和Linux的JDK版本里面才有。
参数很简单,直接查看jstack -h
举例:
jstack pid
JVM监测工具(Java Virtual Machine Statistics Monitoring Tool)。利用了JVM内建的指令对Java应用程序的资源和性能进行实时的命令行的监控,包括各种堆和非堆的大小及其内存使用量、classloader、compiler、垃圾回收状况等。
举例:
jstat –printcompilation -h10 3024 250 600
Usage: jstat -help|-options
jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
参数介绍:
- -h n 每隔几行输出标题
- vmid VM的进程号,即当前运行的java进程号
- -t 在第一列显示自JVM启动以来的时间戳
- -J 修改java进程的参数。类似jinfo -flag <name>=<value>。例如-J-Xms48m 设置初始堆为48M。详见这里。这个参数挺有用的,可以在运行中调整参数以方便测试、监测。
- -option option为要检测的参数。参数列表可通过jstat –options 获取。下面将分别介绍每个参数及输出字段的含义。
| class | 统计class loader行为信息 |
| compiler | 统计编译行为信息 |
| gc | 统计jdk gc时heap信息 |
| gccapacity | 统计堆内存不同代的heap容量信息 |
| gccause | 统计gc的情况(同-gcutil)和引起gc的事件 |
| gcnew | 统计gc时新生代的信息(相比gcutil更详细) |
| gcnewcapacity | 统计gc时新生代heap容量 |
| gcold | 统计gc时,老年区的情况 |
| gcoldcapacity | 统计gc时,老年区heap容量 |
| gcpermcapacity | 统计gc时,permanent区heap容量 |
| gcutil | 统计gc时,heap情况 |
| printcompilation | 统计编译行为信息 |
-class option:Class Loader Statistics
| Column | Description |
|---|---|
| Loaded | Number of classes loaded. |
| Bytes | Number of Kbytes loaded. |
| Unloaded | Number of classes unloaded. |
| Bytes | Number of Kbytes unloaded. |
| Time | Time spent performing class load and unload operations. |
-compiler:HotSpot Just-In-Time Compiler Statistics
| Column | Description |
|---|---|
| Compiled | Number of compilation tasks performed. |
| Failed | Number of compilation tasks that failed. |
| Invalid | Number of compilation tasks that were invalidated. |
| Time | Time spent performing compilation tasks. |
| FailedType | Compile type of the last failed compilation. |
| FailedMethod | Class name and method for the last failed compilation. |
-gc Option:Garbage-collected heap statistics
| Column | Description |
|---|---|
| S0C | Current survivor space 0 capacity (KB). |
| S1C | Current survivor space 1 capacity (KB). |
| S0U | Survivor space 0 utilization (KB). |
| S1U | Survivor space 1 utilization (KB). |
| EC | Current eden space capacity (KB). |
| EU | Eden space utilization (KB). |
| OC | Current old space capacity (KB). |
| OU | Old space utilization (KB). |
| PC | Current permanent space capacity (KB). |
| PU | Permanent space utilization (KB). |
| YGC | Number of young generation GC Events. |
| YGCT | Young generation garbage collection time. |
| FGC | Number of full GC events. |
| FGCT | Full garbage collection time. |
| GCT | Total garbage collection time. |
-gccapacity Option:Memory Pool Generation and Space Capacities
| Column | Description |
|---|---|
| NGCMN | Minimum new generation capacity (KB). |
| NGCMX | Maximum new generation capacity (KB). |
| NGC | Current new generation capacity (KB). |
| S0C | Current survivor space 0 capacity (KB). |
| S1C | Current survivor space 1 capacity (KB). |
| EC | Current eden space capacity (KB). |
| OGCMN | Minimum old generation capacity (KB). |
| OGCMX | Maximum old generation capacity (KB). |
| OGC | Current old generation capacity (KB). |
| OC | Current old space capacity (KB). |
| PGCMN | Minimum permanent generation capacity (KB). |
| PGCMX | Maximum Permanent generation capacity (KB). |
| PGC | Current Permanent generation capacity (KB). |
| PC | Current Permanent space capacity (KB). |
| YGC | Number of Young generation GC Events. |
| FGC | Number of Full GC Events. |
-gccause Option:Garbage Collection Statistics, Including GC Events
| Column | Description |
|---|---|
| LGCC | Cause of last Garbage Collection. |
| GCC | Cause of current Garbage Collection. |
前面的字段与gcutil相同.
-gcnew Option:New Generation Statistics
| Column | Description |
|---|---|
| S0C | Current survivor space 0 capacity (KB). |
| S1C | Current survivor space 1 capacity (KB). |
| S0U | Survivor space 0 utilization (KB). |
| S1U | Survivor space 1 utilization (KB). |
| TT | Tenuring threshold. |
| MTT | Maximum tenuring threshold. |
| DSS | Desired survivor size (KB). |
| EC | Current eden space capacity (KB). |
| EU | Eden space utilization (KB). |
| YGC | Number of young generation GC events. |
| YGCT | Young generation garbage collection time. |
-gcnewcapacity Option:New Generation Space Size Statistics
| Column | Description |
|---|---|
| NGCMN | Minimum new generation capacity (KB). |
| NGCMX | Maximum new generation capacity (KB). |
| NGC | Current new generation capacity (KB). |
| S0CMX | Maximum survivor space 0 capacity (KB). |
| S0C | Current survivor space 0 capacity (KB). |
| S1CMX | Maximum survivor space 1 capacity (KB). |
| S1C | Current survivor space 1 capacity (KB). |
| ECMX | Maximum eden space capacity (KB). |
| EC | Current eden space capacity (KB). |
| YGC | Number of young generation GC events. |
| FGC | Number of Full GC Events. |
-gcold Option:Old and Permanent Generation Statistics
| Column | Description |
|---|---|
| PC | Current permanent space capacity (KB). |
| PU | Permanent space utilization (KB). |
| OC | Current old space capacity (KB). |
| OU | old space utilization (KB). |
| YGC | Number of young generation GC events. |
| FGC | Number of full GC events. |
| FGCT | Full garbage collection time. |
| GCT | Total garbage collection time. |
-gcoldcapacity Option:Old Generation Statistics
| Column | Description |
|---|---|
| OGCMN | Minimum old generation capacity (KB). |
| OGCMX | Maximum old generation capacity (KB). |
| OGC | Current old generation capacity (KB). |
| OC | Current old space capacity (KB). |
| YGC | Number of young generation GC events. |
| FGC | Number of full GC events. |
| FGCT | Full garbage collection time. |
| GCT | Total garbage collection time. |
-gcpermcapacity Option: Permanent Generation Statistics
| Column | Description |
|---|---|
| PGCMN | Minimum permanent generation capacity (KB). |
| PGCMX | Maximum permanent generation capacity (KB). |
| PGC | Current permanent generation capacity (KB). |
| PC | Current permanent space capacity (KB). |
| YGC | Number of young generation GC events. |
| FGC | Number of full GC events. |
| FGCT | Full garbage collection time. |
| GCT | Total garbage collection time. |
-gcutil Option:Summary of Garbage Collection Statistics
| Column | Description |
|---|---|
| S0 | Survivor space 0 utilization as a percentage of the space's current capacity. |
| S1 | Survivor space 1 utilization as a percentage of the space's current capacity. |
| E | Eden space utilization as a percentage of the space's current capacity. |
| O | Old space utilization as a percentage of the space's current capacity. |
| P | Permanent space utilization as a percentage of the space's current capacity. |
| YGC | Number of young generation GC events. |
| YGCT | Young generation garbage collection time. |
| FGC | Number of full GC events. |
| FGCT | Full garbage collection time. |
| GCT | Total garbage collection time. |
-printcompilation Option: HotSpot Compiler Method Statistics
| Column | Description |
|---|---|
| Compiled | Number of compilation tasks performed. |
| Size | Number of bytes of bytecode for the method. |
| Type | Compilation type. |
| Method | Class name and method name identifying the compiled method. Class name uses "/" instead of "." as namespace separator. Method name is the method within the given class. The format for these two fields is consistent with the HotSpot - XX:+PrintComplationoption. |

本文深入探讨了Java开发中常用的内存诊断工具jinfo、jmap、jstack和性能监控工具jstat的使用方法与应用场景,包括如何通过jmap获取内存占用情况、jstack观察线程状态、jstat监控资源和性能,以及如何利用MemoryAnalyzer分析dump文件。
882

被折叠的 条评论
为什么被折叠?



