淺談JDK14性能管理工具之jmap和jhat
簡介
jmap(Java Memory Map)是JDK自帶的工具,用來將某個(gè)java程序的內(nèi)存中的信息打印或者輸出到文件中,然后通過jhat(Java Heap Analysis Tool)工具對(duì)輸出的文件進(jìn)行分析,從而找到可能出現(xiàn)的問題。
接下來進(jìn)入我們的jmap和jhat之旅吧。
jmap
jmap -clstats <pid>
to connect to running process and print class loader statistics
jmap -finalizerinfo <pid>
to connect to running process and print information on objects awaiting finalization
jmap -histo[:[<histo-options>]] <pid>
to connect to running process and print histogram of java object heap
jmap -dump:<dump-options> <pid>
to connect to running process and dump java heap
jmap有下面四個(gè)可用選項(xiàng):
clstats
clstats的全稱叫做class loader statistics,用輸出類加載有關(guān)的統(tǒng)計(jì)信息。
舉一個(gè)例子:
jmap -clstats 8820
輸出結(jié)果如下:
- Index - class的編號(hào)
- Super - 父類的編號(hào)
- InstBytes - 每個(gè)instance的bytes大小
- KlassBytes - 該class的bytes大小
- annotations - 注解大小
- CpAll - 每個(gè)class中constants, tags, cache, 和 operands的大小
- MethodCount - class中方法的個(gè)數(shù)
- Bytecodes - byte codes的大小
- MethodAll - method, CONSTMETHOD, stack map, 和 method data的大小
- ROAll - 可以放到read-only memory中的class元數(shù)據(jù)的大小
- RWAll - 可以放到read/write memory中的class元數(shù)據(jù)大小
- Total - ROAll + RWAll
- ClassName - class name
finalizerinfo
finalizerinfo列出準(zhǔn)備finalization的對(duì)象。
jmap -finalizerinfo 8820
如果沒有對(duì)象等待被finalization,則會(huì)輸出:
No instances waiting for finalization found
histo
histo用來輸出java heap對(duì)象的直方圖??梢约右粋€(gè)live選項(xiàng),用來輸出live的對(duì)象。
jmap -histo:live 8820
輸出結(jié)果:
num是對(duì)象的編號(hào),instances是對(duì)象的個(gè)數(shù),bytes是對(duì)象的大小,class name是對(duì)象的class名字。
dump
最后要講一下dump,dump用于dump整個(gè)java heap,dump可以接三個(gè)參數(shù):
- live - dump live對(duì)象
- format=b - 以hprof的二進(jìn)制模式dump
- file=filename - dump對(duì)象到文件中
jmap -dump:live,file=dump.log 8820
這里dump.log文件是非常大的,用肉眼也很難分析,下面我們介紹一下jhat(Java Heap Analysis Tool)命令來對(duì)dump出來的對(duì)象進(jìn)行分析。
jhat
注意,jhat從JDK9的時(shí)候已經(jīng)刪除了(JEP 241: Remove the jhat Tool)?,F(xiàn)在Oracle官方推薦的分析工具是Eclipse Memory Analyzer Tool (MAT) 和 VisualVM。 這兩款工具后面有時(shí)間再詳細(xì)講解。
今天先使用JDK8中的jhat來分析一下上面dump出來的文件。
先看下jhat的命令格式:
Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>
-J<flag> Pass <flag> directly to the runtime system. For
example, -J-mx512m to use a maximum heap size of 512MB
-stack false: Turn off tracking object allocation call stack.
-refs false: Turn off tracking of references to objects
-port <port>: Set the port for the HTTP server. Defaults to 7000
-exclude <file>: Specify a file that lists data members that should
be excluded from the reachableFrom query.
-baseline <file>: Specify a baseline object dump. Objects in
both heap dumps with the same ID and same class will
be marked as not being "new".
-debug <int>: Set debug level.
0: No debug output
1: Debug hprof file parsing
2: Debug hprof file parsing, no server
因?yàn)檫@個(gè)命令已經(jīng)被廢棄了,這里就不過多講解它的參數(shù),總體來說jhap會(huì)解析dump出來的文件,并在本地啟動(dòng)一個(gè)web服務(wù)器,我們可以通過web頁面來查看dump出來的數(shù)據(jù)。默認(rèn)情況下web服務(wù)器的端口是7000。
jhat dump.log
Reading from dump.log...
Dump file created Mon May 11 21:13:43 CST 2020
Snapshot read, resolving...
Resolving 197989 objects...
Chasing references, expect 39 dots.......................................
Eliminating duplicate references.......................................
Snapshot resolved.
打開localhost:7000,我們可以看到首頁展示的是各個(gè)包中的類的實(shí)例和地址信息:
點(diǎn)擊首頁的類的鏈接,可以跳轉(zhuǎn)到類的具體信息頁面:
類的信息頁面包含很多信息,包括父類,類加載器,簽名,安全域,子類,實(shí)例,引用等詳細(xì)信息。
對(duì)我們分析內(nèi)存泄露和內(nèi)存異常等情況非常有用。!
以上就是淺談JDK14性能管理工具之jmap和jhat的詳細(xì)內(nèi)容,更多關(guān)于JDK14性能管理工具之jmap和jhat的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決程序包org.springframework.test.context不存在
這篇文章主要介紹了解決程序包org.springframework.test.context不存在的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09elasticsearch索引index之engine讀寫控制結(jié)構(gòu)實(shí)現(xiàn)
這篇文章主要為大家介紹了elasticsearch索引index之engine讀寫控制結(jié)構(gòu)實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04在Spring Boot中使用Spark Streaming進(jìn)行實(shí)時(shí)數(shù)據(jù)處理和流式計(jì)算的步驟
這篇文章主要介紹了在Spring Boot中使用Spark Streaming進(jìn)行實(shí)時(shí)數(shù)據(jù)處理和流式計(jì)算,通過本文的介紹,我們了解了在Spring Boot中使用Spark Streaming進(jìn)行實(shí)時(shí)數(shù)據(jù)處理和流式計(jì)算的詳細(xì)步驟,需要的朋友可以參考下2024-03-03SpringMVC實(shí)現(xiàn)前端后臺(tái)交互傳遞數(shù)據(jù)
本篇文章主要介紹了SpringMVC實(shí)現(xiàn)前端后臺(tái)傳遞數(shù)據(jù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Spring配置文件解析之BeanDefinitionParserDelegate詳解
這篇文章主要介紹了Spring配置文件解析之BeanDefinitionParserDelegate詳解,對(duì)于Spring的配置文件的解析處理操作是在BeanDefinitionParserDelegate中進(jìn)行處理操作,接下來我們簡單介紹一下BeanDefinitionParserDelegate所做的處理操作,需要的朋友可以參考下2024-02-02使用eclipse + maven一步步搭建SSM框架教程詳解
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三個(gè)開源框架整合而成,常作為數(shù)據(jù)源較簡單的web項(xiàng)目的框架.這篇文章主要介紹了eclipse + maven搭建SSM框架 ,需要的朋友可以參考下2017-11-11Java代碼中與Lua相互調(diào)用實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Java代碼中與Lua相互調(diào)用實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08