前幾篇篇文章介紹了介紹了JVM的參數設置並給出了一些生產環境的JVM參數配置參考方案。正如以前文章中提到的JVM參數的設置須要根據應用的特性來進行設置,每一個參數的設置都須要對JVM進行長時間的監測,並不斷進行調整才能找到最佳設置方案。本文將介紹若是經過工具及Java api來監測JVM的運行狀態,並詳細介紹各工具的使用方法。html
須要監測的數據:(內存使用狀況 誰使用了內存 GC的情況)java
內存使用狀況--heap&PermGenlinux
@ 表示經過jmap –heap pid 能夠獲取的值api
# 表示經過jstat –gcutil pid 能夠獲取的值服務器
參數的查看能夠經過多種方法 本文中只隨機列出一種。app
描述 | 最大值 | 當前值 | 報警值 |
堆內存 | @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 | 自設 |
內存使用狀況--configeclipse
描述 | 配置值 |
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 heapjvm
誰使用了內存ide
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
觀察運行中的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
每250毫秒打印一次,一共打印600次 每隔10行顯示一次head
Usage: jstat -help|-options
jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
參數介紹:
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:+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 {demo2:自定義Mbean Type,記錄的數據可經過jconsole等工具或自寫代碼查看,
public static void main( String[] args ) throws Exception {
ClassLoadingMXBean bean = ManagementFactory.getClassLoadingMXBean();
System.out.println( bean.getLoadedClassCount() );
}
}
//工具方法
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);
}
}
相關推薦文章:
參考:
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://www.coderanch.com/t/329407/java/java/find-all-loaded-classes-classloaders