JVM系列五:JVM监测工具[整理中]
轉自本站:http://www.cnblogs.com/redcreen/archive/2011/05/09/2040977.html
?前幾篇篇文章介紹了介紹了JVM的參數設置并給出了一些生產環境的JVM參數配置參考方案。正如之前文章中提到的JVM參數的設置需要根據應用的特性來進行設置,每個參數的設置都需要對JVM進行長時間的監測,并不斷進行調整才能找到最佳設置方案。本文將介紹如果通過工具及Java api來監測JVM的運行狀態,并詳細介紹各工具的使用方法。
?????? 需要監測的數據:(內存使用情況 誰使用了內存 GC的狀況)
內存使用情況--heap&PermGen
@ 表示通過jmap –heap?pid?可以獲取的值
# 表示通過jstat –gcutil?pid?可以獲取的值
參數的查看可以通過多種方法 本文中只隨機列出一種。
| 描述 | 最大值 | 當前值 | 報警值 |
| 堆內存 | @Heap Configuration::MaxHeapSize sum(eden+servivor+old) | sum(eden+servivor+old) | 自設 |
| 非堆內存 | sum(perm+native) | ? | 無 |
| Eden | @Eden Space::capacity | @Eden Space::used | 無 |
| Survivor0 | @From Space::capacity | @From Space::used | 無 |
| Survivor1 | @To Space::capacity | @To Space::used | 無 |
| New gen (注意區別于Xmn參數設置) | @New Generation::capacity Eden + 1 Survivor Space | @New Generation::used | 無 |
| Old gen | @concurrent mark-sweep generation::capacity (CMS是對old區的gc,所以此處即表示old gen) | @concurrent mark-sweep generation::capacity(CMS)::used | 自設 |
| Perm Gen | @Perm Generation::capacity | @Perm Generation::used | 自設 |
內存使用情況--config
| 描述 | 配置值 |
| MaxTenuringThreshold | jinfo -flag MaxTenuringThreshold?pid |
| MinHeapFreeRatio | @Heap Configuration::MinHeapFreeRatio |
| MaxHeapFreeRatio | @Heap Configuration::MaxHeapFreeRatio |
| new gen gc | @using … in the new generation |
| old gen gc | new gen gc聲明下方 |
| 類總數統計 | ?? |
內存使用情況—C heap
- top or ps aux
誰使用了內存
- Heap
jmap –histo
jmap –dump ,then mat - C heap
google perftools
GC的狀況
| 描述 | 收集次數 | 收集時間 | 應用暫停時間 |
| Full GC | #FGC | #FGCT | 設置-XX:+PrintGCApplicationStoppedTime后在日志中查看 |
| Young GC | #YGC | #YGCT | 同上 |
-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC??-XX:+PrintGCApplicationStoppedTime? -Xloggc:logs/gc.log
?
常用工具介紹:jinfo jmap jstack jstat
jinfo?
- 可以從一個給定的java進程或core文件或遠程debug服務器上獲取java配置信息。包括java系統屬性及JVM參數(command line flags)。注意在jvm啟動參數中沒有配置的參數也可使用jinfo –flag xxx pid輸出默認值(很有用,但貌似一些簡寫的參數查不出來)。
- 可以修改運行時的java 進程的opts。
- 只有solaris和linux的JDK版本里有。
- 使用方式可使用jinfo –h 查詢。
jmap
?????? 觀察運行中的jvm物理內存的占用情況。
?????? 如果連用SHELL jmap -histo pid>a.log可以將其保存到文本中去,在一段時間后,使用文本對比工具,可以對比出GC回收了哪些對象。
?????? 參數很簡單,直接查看jmap -h
?????? 舉例:
jmap -heap pid
jmap -dump:format=b,file=heap.hprof <pid>
dump文件可以通過MemoryAnalyzer分析查看.網址:http://www.eclipse.org/mat/,可以查看dump時對象數量,內存占用,線程情況等。 jmap -dump:live為啥會觸發Full GCjstack
?????? 觀察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
jstat
?????? JVM監測工具(Java Virtual Machine Statistics Monitoring Tool)。利用了JVM內建的指令對Java應用程序的資源和性能進行實時的命令行的監控,包括各種堆和非堆的大小及其內存使用量、classloader、compiler、垃圾回收狀況等。
舉例:
jstat –printcompilation -h10 3024 250 600???? 每250毫秒打印一次,一共打印600次 每隔10行顯示一次head
語法結構: Usage: jstat -help|-optionsjstat -<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
| 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
| 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
| 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
| 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
| LGCC | Cause of last Garbage Collection. |
| GCC | Cause of current Garbage Collection. |
?????? 前面的字段與gcutil相同.
-gcnew Option:New Generation Statistics
| 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
| 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
| 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
| 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
| 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
| 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
| 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:+PrintComplation?option. |
?
Java api方式監測
?????? jre中提供了一些查看運行中的jvm內部信息的api,這些api包含在java.lang.management包中,此包中的接口是在jdk 5中引入的,所以只有在jdk 5及其以上版本中才能通過這種方式訪問這些信息。下面簡單介紹一下這包括哪些信息,以及如何訪問。
?????? 可以通過此api訪問到運行中的jvm的類加載的信息、jit編譯器的信息、內存分配的情況、線程的相關信息以及運行jvm的操作系統的信息。java.lang.management包中提供了9個接口來訪問這些信息,使用ManagementFactory的靜態get方法可以獲得相應接口的實例,可以通過這些實例來獲取你需要的相關信息。
?????? 更詳細的關于MBean的介紹參見Java SE 6 新特性: JMX 與系統管理
?????? demo1:查看一下當前運行的jvm中加載了多少個類。想詳細了解如何使用這些api,可以參考java.lang.management包中的詳細api文檔。
public class ClassLoaderChecker {public static void main( String[] args ) throws Exception {
????? ClassLoadingMXBean bean = ManagementFactory.getClassLoadingMXBean();
????? System.out.println( bean.getLoadedClassCount() );
??? }
} demo2:自定義Mbean Type,記錄的數據可通過jconsole等工具或自寫代碼查看,
//工具方法
public static ObjectName register(String name, Object mbean) {
try {
ObjectName objectName = new ObjectName(name);
MBeanServer mbeanServer = ManagementFactory
.getPlatformMBeanServer();
try {
mbeanServer.registerMBean(mbean, objectName);
} catch (InstanceAlreadyExistsException ex) {
mbeanServer.unregisterMBean(objectName);
mbeanServer.registerMBean(mbean, objectName);
}
return objectName;
} catch (JMException e) {
throw new IllegalArgumentException(name, e);
}
}
相關推薦文章:
在 Java SE 6 中監視和診斷性能問題
?
參考:
http://www.51testing.com/?uid-183198-action-viewspace-itemid-185174
JVM監控工具介紹jstack, jconsole, jinfo, jmap, jdb, jstat
http://stl-www.htw-saarland.de/syst-lab/java/jdk-1_5_0/docs/tooldocs/share/jinfo.html
http://qa.taobao.com/?p=10010
運用Jconsole監控JVM
http://www.coderanch.com/t/329407/java/java/find-all-loaded-classes-classloaders
轉載于:https://www.cnblogs.com/southdom/articles/5001707.html
總結
以上是生活随笔為你收集整理的JVM系列五:JVM监测工具[整理中]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7安装电子专利CPC客户端以及专利
- 下一篇: 常见的Markdownpad2运行破解以