您好,登錄后才能下訂單哦!
如果通過(guò)工具及Java api來(lái)監(jiān)測(cè)JVM的運(yùn)行狀態(tài),針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
JVM參數(shù)的設(shè)置需要根據(jù)應(yīng)用的特性來(lái)進(jìn)行設(shè)置,每個(gè)參數(shù)的設(shè)置都需要對(duì)JVM進(jìn)行長(zhǎng)時(shí)間的監(jiān)測(cè),并不斷進(jìn)行調(diào)整才能找到***設(shè)置方案。如果通過(guò)工具及Java api來(lái)監(jiān)測(cè)JVM的運(yùn)行狀態(tài),并詳細(xì)介紹各工具的使用方法。
需要監(jiān)測(cè)的數(shù)據(jù):(內(nèi)存使用情況 誰(shuí)使用了內(nèi)存 GC的狀況)
內(nèi)存使用情況--heap&PermGen
@ 表示通過(guò)jmap –heap pid 可以獲取的值
# 表示通過(guò)jstat –gcutil pid 可以獲取的值
參數(shù)的查看可以通過(guò)多種方法 本文中只隨機(jī)列出一種。
描述 | ***值 | 當(dāng)前值 | 報(bào)警值 |
堆內(nèi)存 | @Heap Configuration::MaxHeapSize sum(eden+servivor+old) | sum(eden+servivor+old) | 自設(shè) |
非堆內(nèi)存 | sum(perm+native) | 無(wú) | |
Eden | @Eden Space::capacity | @Eden Space::used | 無(wú) |
Survivor0 | @From Space::capacity | @From Space::used | 無(wú) |
Survivor1 | @To Space::capacity | @To Space::used | 無(wú) |
New gen (注意區(qū)別于Xmn參數(shù)設(shè)置) | @New Generation::capacity Eden + 1 Survivor Space | @New Generation::used | 無(wú) |
Old gen | @concurrent mark-sweep generation::capacity (CMS是對(duì)old區(qū)的gc,所以此處即表示old gen) | @concurrent mark-sweep generation::capacity(CMS)::used | 自設(shè) |
Perm Gen | @Perm Generation::capacity | @Perm Generation::used | 自設(shè) |
內(nèi)存使用情況--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聲明下方 |
類總數(shù)統(tǒng)計(jì) | ?? |
內(nèi)存使用情況—C heap
◆ top or ps aux
誰(shuí)使用了內(nèi)存
◆ Heap
jmap –histo
jmap –dump ,then mat
◆ C heap
google perftools
GC的狀況
描述 | 收集次數(shù) | 收集時(shí)間 | 應(yīng)用暫停時(shí)間 |
Full GC | #FGC | #FGCT | 設(shè)置-XX:+PrintGCApplicationStoppedTime后在日志中查看 |
Young GC | #YGC | #YGCT | 同上 |
-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+PrintGCApplicationStoppedTime -Xloggc:logs/gc.log
常用工具介紹:jinfo jmap jstack jstat
jinfo
◆ 可以從一個(gè)給定的java進(jìn)程或core文件或遠(yuǎn)程debug服務(wù)器上獲取java配置信息。包括java系統(tǒng)屬性及JVM參數(shù)(command line flags)。注意在jvm啟動(dòng)參數(shù)中沒(méi)有配置的參數(shù)也可使用jinfo –flag xxx pid輸出默認(rèn)值(很有用,但貌似一些簡(jiǎn)寫(xiě)的參數(shù)查不出來(lái))。
◆ 可以修改運(yùn)行時(shí)的java 進(jìn)程的opts。
◆ 只有solaris和linux的JDK版本里有。
◆ 使用方式可使用jinfo –h 查詢。
jmap
觀察運(yùn)行中的jvm物理內(nèi)存的占用情況。
如果連用SHELL jmap -histo pid>a.log可以將其保存到文本中去,在一段時(shí)間后,使用文本對(duì)比工具,可以對(duì)比出GC回收了哪些對(duì)象。
參數(shù)很簡(jiǎn)單,直接查看jmap -h
舉例:
jmap -heap pid jmap -dump:format=b,file=heap.hprof <pid>
dump文件可以通過(guò)MemoryAnalyzer分析查看.網(wǎng)址:http://www.eclipse.org/mat/,可以查看dump時(shí)對(duì)象數(shù)量,內(nèi)存占用,線程情況等。
jmap -dump:live為啥會(huì)觸發(fā)Full GC
jstack
觀察jvm中當(dāng)前所有線程的運(yùn)行情況和線程當(dāng)前狀態(tài)
如果java程序崩潰生成core文件,jstack工具可以用來(lái)獲得core文件的java stack和native stack的信息,從而可以輕松地知道java程序是如何崩潰和在程序何處發(fā)生問(wèn)題。另外,jstack工具還可以附屬到正在運(yùn)行的java程序中,看到當(dāng)時(shí)運(yùn)行的java程序的java stack和native stack的信息, 如果現(xiàn)在運(yùn)行的java程序呈現(xiàn)hung的狀態(tài),jstack是非常有用的。目前只有在Solaris和Linux的JDK版本里面才有。
參數(shù)很簡(jiǎn)單,直接查看jstack -h
舉例:
jstack pid
jstat
JVM監(jiān)測(cè)工具(Java Virtual Machine Statistics Monitoring Tool)。利用了JVM內(nèi)建的指令對(duì)Java應(yīng)用程序的資源和性能進(jìn)行實(shí)時(shí)的命令行的監(jiān)控,包括各種堆和非堆的大小及其內(nèi)存使用量、classloader、compiler、垃圾回收狀況等。
舉例:
jstat –printcompilation -h20 3024 250 600
每250毫秒打印一次,一共打印600次 每隔10行顯示一次head
語(yǔ)法結(jié)構(gòu):
Usage: jstat -help|-options jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
參數(shù)介紹:
◆ -h n 每隔幾行輸出標(biāo)題
◆ vmid VM的進(jìn)程號(hào),即當(dāng)前運(yùn)行的java進(jìn)程號(hào)
◆ -t 在***列顯示自JVM啟動(dòng)以來(lái)的時(shí)間戳
◆ -J 修改java進(jìn)程的參數(shù)。類似jinfo -flag <name>=<value>。例如-J-Xms48m 設(shè)置初始堆為48M。詳見(jiàn)這里。這個(gè)參數(shù)挺有用的,可以在運(yùn)行中調(diào)整參數(shù)以方便測(cè)試、監(jiān)測(cè)。
◆ -option option為要檢測(cè)的參數(shù)。參數(shù)列表可通過(guò)jstat –options 獲取。下面將分別介紹每個(gè)參數(shù)及輸出字段的含義。
class | 統(tǒng)計(jì)class loader行為信息 |
compiler | 統(tǒng)計(jì)編譯行為信息 |
gc | 統(tǒng)計(jì)jdk gc時(shí)heap信息 |
gccapacity | 統(tǒng)計(jì)堆內(nèi)存不同代的heap容量信息 |
gccause | 統(tǒng)計(jì)gc的情況(同-gcutil)和引起gc的事件 |
gcnew | 統(tǒng)計(jì)gc時(shí)新生代的信息(相比gcutil更詳細(xì)) |
gcnewcapacity | 統(tǒng)計(jì)gc時(shí)新生代heap容量 |
gcold | 統(tǒng)計(jì)gc時(shí),老年區(qū)的情況 |
gcoldcapacity | 統(tǒng)計(jì)gc時(shí),老年區(qū)heap容量 |
gcpermcapacity | 統(tǒng)計(jì)gc時(shí),permanent區(qū)heap容量 |
gcutil | 統(tǒng)計(jì)gc時(shí),heap情況 |
printcompilation | 統(tǒng)計(jì)編譯行為信息 |
-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方式監(jiān)測(cè)
jre中提供了一些查看運(yùn)行中的jvm內(nèi)部信息的api,這些api包含在java.lang.management包中,此包中的接口是在jdk 5中引入的,所以只有在jdk 5及其以上版本中才能通過(guò)這種方式訪問(wèn)這些信息。下面簡(jiǎn)單介紹一下這包括哪些信息,以及如何訪問(wèn)。
可以通過(guò)此api訪問(wèn)到運(yùn)行中的jvm的類加載的信息、jit編譯器的信息、內(nèi)存分配的情況、線程的相關(guān)信息以及運(yùn)行jvm的操作系統(tǒng)的信息。java.lang.management包中提供了9個(gè)接口來(lái)訪問(wèn)這些信息,使用ManagementFactory的靜態(tài)get方法可以獲得相應(yīng)接口的實(shí)例,可以通過(guò)這些實(shí)例來(lái)獲取你需要的相關(guān)信息。
更詳細(xì)的關(guān)于MBean的介紹參見(jiàn)Java SE 6 新特性: JMX 與系統(tǒng)管理
demo1:查看一下當(dāng)前運(yùn)行的jvm中加載了多少個(gè)類。想詳細(xì)了解如何使用這些api,可以參考java.lang.management包中的詳細(xì)api文檔。
public class ClassLoaderChecker { public static void main( String[] args ) throws Exception { ClassLoadingMXBean bean = ManagementFactory.getClassLoadingMXBean(); System.out.println( bean.getLoadedClassCount() ); } }
demo2:自定義Mbean Type,記錄的數(shù)據(jù)可通過(guò)jconsole等工具或自寫(xiě)代碼查看,
//工具方法 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); } } //步驟一:定義Mbean接口: //隨便定義 public interface DemoMBean { public AtomicLong getInvokeCount(); } //步驟二:實(shí)現(xiàn)接口,并注冊(cè): public class DemoImpl implements DemoMBean{ public final static String DEFAULT_OBJECT_NAME_PREFIX = "com.redcreen.demo:type=demo"; register("com.redcreen.demo:type=demo",DemoImpl.instance); } //可以通過(guò)jconsole中查看數(shù)據(jù)了
關(guān)于如果通過(guò)工具及Java api來(lái)監(jiān)測(cè)JVM的運(yùn)行狀態(tài)問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。