溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如果通過(guò)工具及Java api來(lái)監(jiān)測(cè)JVM的運(yùn)行狀態(tài)

發(fā)布時(shí)間:2021-10-23 17:09:20 來(lái)源:億速云 閱讀:151 作者:柒染 欄目:編程語(yǔ)言

如果通過(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

描述配置值
MaxTenuringThresholdjinfo -flag MaxTenuringThreshold pid
MinHeapFreeRatio@Heap Configuration::MinHeapFreeRatio
MaxHeapFreeRatio@Heap Configuration::MaxHeapFreeRatio
new gen gc@using … in the new generation
old gen gcnew 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 &ndash;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 &ndash;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

ColumnDescription
LoadedNumber of classes loaded.
BytesNumber of Kbytes loaded.
UnloadedNumber of classes unloaded.
BytesNumber of Kbytes unloaded.
TimeTime spent performing class load and unload operations.

-compiler:HotSpot Just-In-Time Compiler Statistics

ColumnDescription
CompiledNumber of compilation tasks performed.
FailedNumber of compilation tasks that failed.
InvalidNumber of compilation tasks that were invalidated.
TimeTime spent performing compilation tasks.
FailedTypeCompile type of the last failed compilation.
FailedMethodClass name and method for the last failed compilation.

-gc Option:Garbage-collected heap statistics

ColumnDescription
S0CCurrent survivor space 0 capacity (KB).
S1CCurrent survivor space 1 capacity (KB).
S0USurvivor space 0 utilization (KB).
S1USurvivor space 1 utilization (KB).
ECCurrent eden space capacity (KB).
EUEden space utilization (KB).
OCCurrent old space capacity (KB).
OUOld space utilization (KB).
PCCurrent permanent space capacity (KB).
PUPermanent space utilization (KB).
YGCNumber of young generation GC Events.
YGCTYoung generation garbage collection time.
FGCNumber of full GC events.
FGCTFull garbage collection time.
GCTTotal garbage collection time.

-gccapacity Option:Memory Pool Generation and Space Capacities

ColumnDescription
NGCMNMinimum new generation capacity (KB).
NGCMXMaximum new generation capacity (KB).
NGCCurrent new generation capacity (KB).
S0CCurrent survivor space 0 capacity (KB).
S1CCurrent survivor space 1 capacity (KB).
ECCurrent eden space capacity (KB).
OGCMNMinimum old generation capacity (KB).
OGCMXMaximum old generation capacity (KB).
OGCCurrent old generation capacity (KB).
OCCurrent old space capacity (KB).
PGCMNMinimum permanent generation capacity (KB).
PGCMXMaximum Permanent generation capacity (KB).
PGCCurrent Permanent generation capacity (KB).
PCCurrent Permanent space capacity (KB).
YGCNumber of Young generation GC Events.
FGCNumber of Full GC Events.

-gccause Option:Garbage Collection Statistics, Including GC Events

ColumnDescription
LGCCCause of last Garbage Collection.
 
GCCCause of current Garbage Collection.
 

前面的字段與gcutil相同.

-gcnew Option:New Generation Statistics

ColumnDescription
S0CCurrent survivor space 0 capacity (KB).
S1CCurrent survivor space 1 capacity (KB).
S0USurvivor space 0 utilization (KB).
S1USurvivor space 1 utilization (KB).
TTTenuring threshold.
MTTMaximum tenuring threshold.
DSSDesired survivor size (KB).
ECCurrent eden space capacity (KB).
EUEden space utilization (KB).
YGCNumber of young generation GC events.
YGCTYoung generation garbage collection time.

-gcnewcapacity Option:New Generation Space Size Statistics

ColumnDescription
NGCMN
 
Minimum new generation capacity (KB).
 
NGCMXMaximum new generation capacity (KB).
NGCCurrent new generation capacity (KB).
S0CMXMaximum survivor space 0 capacity (KB).
S0CCurrent survivor space 0 capacity (KB).
S1CMXMaximum survivor space 1 capacity (KB).
S1CCurrent survivor space 1 capacity (KB).
ECMXMaximum eden space capacity (KB).
ECCurrent eden space capacity (KB).
YGCNumber of young generation GC events.
FGCNumber of Full GC Events.

-gcold Option:Old and Permanent Generation Statistics

ColumnDescription
PCCurrent permanent space capacity (KB).
PUPermanent space utilization (KB).
OCCurrent old space capacity (KB).
OUold space utilization (KB).
YGCNumber of young generation GC events.
FGCNumber of full GC events.
FGCTFull garbage collection time.
GCTTotal garbage collection time.

-gcoldcapacity Option:Old Generation Statistics

ColumnDescription
OGCMNMinimum old generation capacity (KB).
OGCMXMaximum old generation capacity (KB).
OGCCurrent old generation capacity (KB).
OCCurrent old space capacity (KB).
YGCNumber of young generation GC events.
FGCNumber of full GC events.
FGCTFull garbage collection time.
GCTTotal garbage collection time.

-gcpermcapacity Option: Permanent Generation Statistics

ColumnDescription
PGCMNMinimum permanent generation capacity (KB).
PGCMXMaximum permanent generation capacity (KB).
PGCCurrent permanent generation capacity (KB).
PCCurrent permanent space capacity (KB).
YGCNumber of young generation GC events.
FGCNumber of full GC events.
FGCTFull garbage collection time.
GCTTotal garbage collection time.

-gcutil Option:Summary of Garbage Collection Statistics

ColumnDescription
S0Survivor space 0 utilization as a percentage of the space's current capacity.
S1Survivor space 1 utilization as a percentage of the space's current capacity.
EEden space utilization as a percentage of the space's current capacity.
OOld space utilization as a percentage of the space's current capacity.
PPermanent space utilization as a percentage of the space's current capacity.
YGCNumber of young generation GC events.
YGCTYoung generation garbage collection time.
FGCNumber of full GC events.
FGCTFull garbage collection time.
GCTTotal garbage collection time.

-printcompilation Option: HotSpot Compiler Method Statistics

ColumnDescription
CompiledNumber of compilation tasks performed.
SizeNumber of bytes of bytecode for the method.
TypeCompilation type.
MethodClass 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í)。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI