JDK14性能管理工具:jmap和jhat使用介绍
文章目錄
- 簡介
- jmap
- clstats
- finalizerinfo
- histo
- dump
- jhat
- 總結(jié)
簡介
我們在寫代碼的過程中,經(jīng)常會遇到內(nèi)存泄露的問題,比如某個集合中的對象沒有被回收,或者內(nèi)存出現(xiàn)不明原因的增長。這些都是需要我們來定位的問題,我們可以使用jmap和jhat來對java程序中的內(nèi)存對象進行分析。
jmap(Java Memory Map)是JDK自帶的工具,用來將某個java程序的內(nèi)存中的信息打印或者輸出到文件中,然后通過jhat(Java Heap Analysis Tool)工具對輸出的文件進行分析,從而找到可能出現(xiàn)的問題。
更多精彩內(nèi)容且看:
- 區(qū)塊鏈從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特幣等持續(xù)更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續(xù)更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續(xù)更新
- java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細文章教程
更多內(nèi)容請訪問www.flydean.com
接下來進入我們的jmap和jhat之旅吧。
jmap
jmap -clstats <pid>to connect to running process and print class loader statisticsjmap -finalizerinfo <pid>to connect to running process and print information on objects awaiting finalizationjmap -histo[:[<histo-options>]] <pid>to connect to running process and print histogram of java object heapjmap -dump:<dump-options> <pid>to connect to running process and dump java heapjmap有下面四個可用選項:
clstats
clstats的全稱叫做class loader statistics,用輸出類加載有關的統(tǒng)計信息。
舉一個例子:
jmap -clstats 8820輸出結(jié)果如下:
- Index - class的編號
- Super - 父類的編號
- InstBytes - 每個instance的bytes大小
- KlassBytes - 該class的bytes大小
- annotations - 注解大小
- CpAll - 每個class中constants, tags, cache, 和 operands的大小
- MethodCount - class中方法的個數(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列出準備finalization的對象。
jmap -finalizerinfo 8820如果沒有對象等待被finalization,則會輸出:
No instances waiting for finalization foundhisto
histo用來輸出java heap對象的直方圖??梢约右粋€live選項,用來輸出live的對象。
jmap -histo:live 8820輸出結(jié)果:
num是對象的編號,instances是對象的個數(shù),bytes是對象的大小,class name是對象的class名字。
dump
最后要講一下dump,dump用于dump整個java heap,dump可以接三個參數(shù):
- live - dump live對象
- format=b - 以hprof的二進制模式dump
- file=filename - dump對象到文件中
這里dump.log文件是非常大的,用肉眼也很難分析,下面我們介紹一下jhat(Java Heap Analysis Tool)命令來對dump出來的對象進行分析。
jhat
注意,jhat從JDK9的時候已經(jīng)刪除了(JEP 241: Remove the jhat Tool)?,F(xiàn)在Oracle官方推薦的分析工具是Eclipse Memory Analyzer Tool (MAT) 和 VisualVM。 這兩款工具后面有時間再詳細講解。
今天先使用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. Forexample, -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 shouldbe excluded from the reachableFrom query.-baseline <file>: Specify a baseline object dump. Objects inboth heap dumps with the same ID and same class willbe marked as not being "new".-debug <int>: Set debug level.0: No debug output1: Debug hprof file parsing2: Debug hprof file parsing, no server因為這個命令已經(jīng)被廢棄了,這里就不過多講解它的參數(shù),總體來說jhap會解析dump出來的文件,并在本地啟動一個web服務器,我們可以通過web頁面來查看dump出來的數(shù)據(jù)。默認情況下web服務器的端口是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,我們可以看到首頁展示的是各個包中的類的實例和地址信息:
點擊首頁的類的鏈接,可以跳轉(zhuǎn)到類的具體信息頁面:
類的信息頁面包含很多信息,包括父類,類加載器,簽名,安全域,子類,實例,引用等詳細信息。
對我們分析內(nèi)存泄露和內(nèi)存異常等情況非常有用。
總結(jié)
本文介紹了jmap和jhat的使用。
本文作者:flydean程序那些事
本文鏈接:http://www.flydean.com/jdk14-jmap-jhat/
本文來源:flydean的博客
歡迎關注我的公眾號:程序那些事,更多精彩等著您!
總結(jié)
以上是生活随笔為你收集整理的JDK14性能管理工具:jmap和jhat使用介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JDK14性能管理工具:jstat使用介
- 下一篇: 你不知道的java对象序列化的秘密