Java如何生成Heap Dump及OOM問題排查

Heap Dump簡述

Heap dump文件是一個二進制文件略步,它保存了某一時刻JVM堆中對象使用情況谅猾。Heap Dump文件是指定時刻的Java堆棧的快照,是一種鏡像文件嫉到。Heap Dump一般都包含了一個堆中的Java Objects, Class等基本信息。同時月洛,當你在執(zhí)行一個轉(zhuǎn)儲操作時何恶,往往會觸發(fā)一次GC,所以你轉(zhuǎn)儲得到的文件里包含的信息通常是有效的內(nèi)容(包含比較少嚼黔,或沒有垃圾對象了) 细层。我們可以這么理解:heap dump記錄內(nèi)存信息的,thread dump是記錄CPU信息的唬涧。

Heap Dump 包含的信息

  • 所有的對象信息
    對象的類信息疫赎、字段信息、原生值(int, long等)及引用值
  • 所有的類信息
    類加載器碎节、類名捧搞、超類及靜態(tài)字段
  • 垃圾回收的根對象
    根對象是指那些可以直接被虛擬機觸及的對象
  • 線程棧及局部變量
    包含了轉(zhuǎn)儲時刻的線程調(diào)用棧信息和棧幀中的局部變量信息

Heap Dump 獲取方式

1. 使用 jmap 命令生成 dump 文件

jmap -dump:live,format=b,file=c:\dump\heap.hprof <pid>

eg. 1) 在windows主機上面通過tasklist獲取進程pid

C:\Program Files (x86)\PowerCmd>tasklist

映像名稱                       PID 會話名              會話#       內(nèi)存使用 
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0          8 K
System                           4 Services                   0      1,256 K
Registry                        96 Services                   0     22,608 K
smss.exe                       448 Services                   0      1,028 K
csrss.exe                      656 Services                   0      4,532 K
wininit.exe                    760 Services                   0      5,764 K
csrss.exe                      768 Console                    1      5,088 K
services.exe                   832 Services                   0      9,912 K
lsass.exe                      840 Services                   0     16,356 K
svchost.exe                    960 Services                   0      3,816 K
svchost.exe                    984 Services                   0     26,528 K
fontdrvhost.exe                 68 Services                   0      3,116 K
svchost.exe                    524 Services                   0     13,408 K
svchost.exe                    904 Services                   0      7,524 K
winlogon.exe                  1104 Console                    1      8,312 K
fontdrvhost.exe               1160 Console                    1     21,644 K
dwm.exe                       1244 Console                    1     84,076 K
aesm_service.exe              1308 Services                   0      6,568 K
svchost.exe                   1372 Services                   0      9,288 K
svchost.exe                   1380 Services                   0     11,288 K
svchost.exe                   1444 Services                   0     13,780 K
svchost.exe                   1504 Services                   0      5,780 K
svchost.exe                   1548 Services                   0     10,036 K
svchost.exe                   1632 Services                   0      9,492 K
svchost.exe                   1716 Services                   0     14,568 K
svchost.exe                   1760 Services                   0      8,804 K
svchost.exe                   1768 Services                   0     14,340 K
ibmpmsvc.exe                  1852 Services                   0      4,920 K
LPlatSvc.exe                  1864 Services                   0      6,768 K
NVDisplay.Container.exe       2036 Services                   0      9,180 K
svchost.exe                   1020 Services                   0      8,372 K
AliSystemSrv.exe              1736 Services                   0      5,536 K
svchost.exe                   2084 Services                   0      7,400 K
NVDisplay.Container.exe       2156 Console                    1     17,744 K
svchost.exe                   2240 Services                   0     11,468 K
svchost.exe                   2324 Services                   0      9,508 K
svchost.exe                   2332 Services                   0     12,056 K
svchost.exe                   2440 Services                   0     12,204 K
svchost.exe                   2448 Services                   0      5,672 K
svchost.exe                   2456 Services                   0      7,620 K
svchost.exe                   2540 Services                   0      7,636 K
Memory Compression            2548 Services                   0     20,356 K
igfxCUIService.exe            2600 Services                   0      7,368 K
svchost.exe                   2660 Services                   0      7,440 K
svchost.exe                   2668 Services                   0     11,972 K
svchost.exe                   2800 Services                   0     11,972 K
RtkAudioService64.exe         2876 Services                   0      6,016 K
svchost.exe                   2884 Services                   0     17,460 K
svchost.exe                   3024 Services                   0      6,236 K
svchost.exe                   3032 Services                   0      8,416 K
svchost.exe                   3040 Services                   0     11,056 K
svchost.exe                   3124 Services                   0     12,428 K
audiodg.exe                   3196 Services                   0     31,912 K
svchost.exe                   3292 Services                   0     14,592 K
sihost.exe                    3332 Console                    1     24,480 K
svchost.exe                   3356 Console                    1     21,252 K
PresentationFontCache.exe     3404 Services                   0     23,492 K
svchost.exe                   3492 Console                    1     32,452 K
svchost.exe                   3500 Services                   0     11,212 K
spoolsv.exe                   3660 Services                   0     11,104 K
TpShocks.exe                  3668 Console                    1      1,308 K
taskhostw.exe                 3736 Console                    1     16,028 K
wlanext.exe                   3820 Services                   0     12,700 K
svchost.exe                   3828 Services                   0      7,920 K
conhost.exe                   3852 Services                   0      4,832 K
svchost.exe                   3984 Services                   0     28,616 K
svchost.exe                   3992 Services                   0      7,400 K
ctfmon.exe                    4064 Console                    1     24,704 K
explorer.exe                  4104 Console                    1    145,340 K
WmiPrvSE.exe                  4308 Services                   0     16,784 K
AlibabaProtect.exe            4420 Services                   0     78,640 K
IntelCpHDCPSvc.exe            4428 Services                   0      6,636 K
svchost.exe                   4436 Services                   0      5,888 K
svchost.exe                   4444 Services                   0     24,352 K
OfficeClickToRun.exe          4464 Services                   0     21,692 K
svchost.exe                   4472 Services                   0     13,008 K
svchost.exe                   4484 Services                   0     29,204 K
EvtEng.exe                    4492 Services                   0     11,484 K
FoxitProtect.exe              4500 Services                   0     14,080 K
ibtsiva.exe                   4540 Services                   0      3,964 K
GfExperienceService.exe       4548 Services                   0      9,816 K
iNodeMon.exe                  4592 Services                   0      8,988 K
svchost.exe                   4624 Services                   0     11,688 K
micmute.exe                   4740 Services                   0      9,532 K
svchost.exe                   4764 Services                   0      8,124 K
mysqld.exe                    4772 Services                   0     23,888 K
NvNetworkService.exe          4788 Services                   0      9,484 K
svchost.exe                   4796 Services                   0      9,896 K
QQProtect.exe                 4824 Services                   0     14,552 K
pcas.exe                      4832 Services                   0     13,752 K
RegSrvc.exe                   4892 Services                   0      8,400 K
SecurityHealthService.exe     4924 Services                   0     11,672 K
SynTPEnhService.exe           4976 Services                   0      8,300 K
svchost.exe                   5032 Services                   0      5,404 K
tphkload.exe                  5040 Services                   0     10,328 K
TeamViewer_Service.exe        5048 Services                   0     13,400 K
MsMpEng.exe                   5068 Services                   0    156,560 K
svchost.exe                   5108 Services                   0     20,796 K
ZeroConfigService.exe         5140 Services                   0     12,708 K
SynTPEnh.exe                  5336 Console                    1     16,524 K
iNodeImg.exe                  5396 Console                    1      6,608 K
conhost.exe                   5480 Console                    1      5,076 K
igfxEM.exe                    5880 Console                    1     10,736 K
svchost.exe                   6008 Services                   0      7,368 K
svchost.exe                   5192 Services                   0      7,012 K
dasHost.exe                   5448 Services                   0      4,644 K
WmiPrvSE.exe                  6304 Services                   0     10,932 K
IntelCpHeciSvc.exe            6400 Services                   0      9,452 K
svchost.exe                   6600 Services                   0      5,708 K
iNodeCmn.exe                  6908 Console                    1     12,768 K
iNodeSec.exe                  6916 Console                    1     10,240 K
conhost.exe                   6948 Console                    1      5,084 K
conhost.exe                   6964 Console                    1      5,096 K
iNodeSslvpn.exe               6980 Console                    1     10,536 K
iNodeL2tpIPSecvpn.exe         6996 Console                    1     10,184 K
conhost.exe                   7020 Console                    1      5,084 K
conhost.exe                   7036 Console                    1      5,080 K
svchost.exe                   7048 Services                   0      7,812 K
svchost.exe                   7296 Services                   0      6,956 K
SynTPHelper.exe               7684 Console                    1      4,216 K
unsecapp.exe                  8124 Services                   0      6,508 K
tposd.exe                     8404 Console                    1     11,380 K
shtctky.exe                   8412 Console                    1      7,776 K
rundll32.exe                  8940 Console                    1      4,556 K
svchost.exe                   9076 Services                   0     15,240 K
ShellExperienceHost.exe       8344 Console                    1     71,196 K
SearchUI.exe                  9248 Console                    1     66,488 K
RuntimeBroker.exe             9424 Console                    1     22,232 K
TSVNCache.exe                 9476 Console                    1     10,896 K
RuntimeBroker.exe             9688 Console                    1     27,068 K
NisSrv.exe                    9864 Services                   0     10,104 K
SettingSyncHost.exe          10180 Console                    1      5,600 K
Video.UI.exe                  3644 Console                    1     16,324 K
svchost.exe                   3172 Services                   0      9,772 K
SkypeHost.exe                10292 Console                    1     32,116 K
LockApp.exe                  10368 Console                    1     29,224 K
svchost.exe                  10428 Services                   0     15,640 K
RuntimeBroker.exe            10500 Console                    1     27,780 K
SearchIndexer.exe            10748 Services                   0     45,120 K
GoogleCrashHandler.exe       10464 Services                   0      1,228 K
GoogleCrashHandler64.exe     10100 Services                   0        912 K
RuntimeBroker.exe             3628 Console                    1     19,016 K
igfxext.exe                   8672 Console                    1      7,968 K
svchost.exe                  10268 Console                    1     21,696 K
RuntimeBroker.exe            11552 Console                    1     16,036 K
RuntimeBroker.exe            11804 Console                    1      7,096 K
MySQLNotifier.exe            11880 Console                    1     41,156 K
RuntimeBroker.exe            11976 Console                    1     13,180 K
svchost.exe                  12016 Services                   0     29,620 K
Shadowsocks.exe              12084 Console                    1     12,460 K
ss_privoxy.exe                5172 Console                    1      8,336 K
jusched.exe                  11936 Console                    1     15,572 K
RAVBg64.exe                   9844 Console                    1      2,172 K
RAVCpl64.exe                 11540 Console                    1      3,184 K
svchost.exe                   8880 Services                   0      7,168 K
ApplicationFrameHost.exe     11284 Console                    1     31,024 K
WinStore.App.exe              1672 Console                    1     36,976 K
Calculator.exe               11392 Console                    1     20,856 K
RuntimeBroker.exe             9004 Console                    1      5,840 K
dllhost.exe                   9440 Console                    1      9,624 K
sppsvc.exe                    3616 Services                   0     12,000 K
svchost.exe                   4116 Services                   0     13,868 K
jhi_service.exe              11724 Services                   0      6,284 K
LMS.exe                      11660 Services                   0     10,900 K
SgrmBroker.exe               12100 Services                   0      5,040 K
svchost.exe                   9528 Services                   0      8,528 K
AlibabaprotectUI.exe          4208 Console                    1     21,368 K
svchost.exe                   5896 Services                   0      8,380 K
svchost.exe                   9380 Services                   0     10,940 K
svchost.exe                   6156 Services                   0      5,352 K
WeChat.exe                   12480 Console                    1     71,152 K
WeChatWeb.exe                 8952 Console                    1     18,216 K
DingTalk.exe                 13052 Console                    1    121,000 K
DingTalkHelper.exe           12588 Console                    1    109,936 K
DingTalk.exe                  3952 Console                    1     10,964 K
cloudmusic.exe               12372 Console                    1     46,304 K
cloudmusic.exe               13348 Console                    1     75,912 K
cloudmusic.exe               13464 Console                    1     65,080 K
QQ.exe                        5772 Console                    1    244,204 K
TXPlatform.exe               13780 Console                    1      2,952 K
WindowsInternal.Composabl     5164 Console                    1     25,592 K
SogouCloud.exe               13812 Console                    1     16,908 K
QQExternal.exe               13720 Console                    1     52,072 K
svchost.exe                   4844 Services                   0      7,012 K
svchost.exe                   8904 Services                   0      9,836 K
FoxitReaderPlus.exe          13580 Console                    1     54,888 K
SogouImeBroker.exe           13892 Console                    1     20,252 K
chrome.exe                    5028 Console                    1    166,544 K
chrome.exe                    3848 Console                    1      7,600 K
chrome.exe                    3060 Console                    1      7,916 K
chrome.exe                   14000 Console                    1     62,864 K
chrome.exe                   12336 Console                    1     60,712 K
chrome.exe                   13932 Console                    1     30,800 K
chrome.exe                   13924 Console                    1     29,288 K
idea64.exe                    3720 Console                    1    760,276 K
fsnotifier64.exe             14712 Console                    1      3,976 K
conhost.exe                  12672 Console                    1      5,120 K
java.exe                     12636 Console                    1     33,100 K
conhost.exe                  14392 Console                    1      5,220 K
ChsIME.exe                   16240 Console                    1     13,700 K
PowerCmd.exe                 14980 Console                    1     53,128 K
cmd.exe                      14864 Console                    1      6,324 K
conhost.exe                   4876 Console                    1      9,164 K
cmd.exe                        200 Console                    1      6,332 K
conhost.exe                  15432 Console                    1      6,544 K
cmd.exe                      16276 Console                    1      6,352 K
conhost.exe                  10868 Console                    1      6,520 K
cmd.exe                      15252 Console                    1      6,328 K
conhost.exe                  16316 Console                    1      6,652 K
java.exe                     13676 Console                    1    585,804 K
java.exe                      9304 Console                    1     65,792 K
java.exe                     15352 Console                    1     88,924 K
java.exe                      8316 Console                    1     68,084 K
WINWORD.EXE                   4200 Console                    1     80,792 K
SecureCRT.exe                14384 Console                    1     43,312 K
chrome.exe                    1388 Console                    1     15,512 K
chrome.exe                    1076 Console                    1     88,620 K
chrome.exe                    1684 Console                    1     43,932 K
chrome.exe                    6316 Console                    1     40,980 K
chrome.exe                   13708 Console                    1     74,120 K
visualvm.exe                  6480 Console                    1      5,920 K
javaw.exe                    10148 Console                    1    121,552 K
jprofiler.exe                16224 Console                    1    558,216 K
chrome.exe                    9300 Console                    1     43,528 K
chrome.exe                    2368 Console                    1     46,320 K
chrome.exe                   12280 Console                    1     47,424 K
chrome.exe                    9652 Console                    1     40,108 K
notepad++.exe                 4060 Console                    1     23,644 K
chrome.exe                   10604 Console                    1     67,932 K
taskhostw.exe                 3216 Console                    1     13,864 K
wwbizsrv.exe                  1792 Services                   0     10,988 K
LPlatSvc.exe                  9936 Console                    1      6,268 K
svchost.exe                    544 Services                   0      5,764 K
cmd.exe                      11996 Console                    1      7,460 K
conhost.exe                   9940 Console                    1     11,240 K
chrome.exe                    1324 Console                    1     69,424 K
chrome.exe                    9484 Console                    1     82,132 K
SearchProtocolHost.exe       10832 Services                   0     13,680 K
cmd.exe                       7364 Console                    1      7,504 K
conhost.exe                   8796 Console                    1     11,136 K
SearchProtocolHost.exe         232 Console                    1      7,912 K
chrome.exe                    3520 Console                    1     21,396 K
java.exe                     10552 Console                    1    107,688 K
java.exe                     13444 Console                    1  1,125,320 K
conhost.exe                   8356 Console                    1      9,820 K
conhost.exe                   9408 Console                    1      9,808 K
SearchFilterHost.exe         14588 Services                   0      9,404 K
jmap.exe                     15580 Console                    1     27,840 K
tasklist.exe                  1280 Console                    1      9,284 K
  1. 使用jmap命令生成heap dump文件
C:\ProgramFiles\Java\jdk1.8.0_144\bin>jmap -dump:live,format=b,file=c:\dumptest\heap1.hprof 13444
Dumping heap to C:\dumptest\heap1.hprof ...
Heap dump file created
  1. 使用使用jprofiler打開生成的heap1.hprof文件
    image.png

    如上圖所示,紅色圈出來的即為占用內(nèi)存最多的對象狮荔,也是最可能發(fā)生OOM問題的地方胎撇;

2. 使用 jcmd 命令生成 dump 文件

jcmd <pid> GC.heap_dump c:\dump\heap.hprof

3. 使用 JVM 參數(shù)獲取 dump 文件

1. -XX:+HeapDumpOnOutOfMemoryError
當OutOfMemoryError發(fā)生時自動生成 Heap Dump 文件。
這可是一個非常有用的參數(shù)轴合,因為當你需要分析Java內(nèi)存使用情況時创坞,往往是在OOM(OutOfMemoryError)發(fā)生時。

eg.


image.png

image.png

image.png
  1. -XX:+HeapDumpBeforeFullGC
    當 JVM 執(zhí)行 FullGC 前執(zhí)行 dump受葛。
  1. -XX:+HeapDumpAfterFullGC
    當 JVM 執(zhí)行 FullGC 后執(zhí)行 dump题涨。
  1. -XX:+HeapDumpOnCtrlBreak
    交互式獲取dump。在控制臺按下快捷鍵Ctrl + Break時总滩,JVM就會轉(zhuǎn)存一下堆快照纲堵。
  1. -XX:HeapDumpPath=c:\test.hprof
    指定 dump 文件存儲路徑。

注意:JVM 生成 Heap Dump 的時候闰渔,虛擬機是暫停一切服務(wù)的席函。如果是線上系統(tǒng)執(zhí)行 Heap Dump 時需要注意。

4. 使用其它工具獲取dump文件

分析 Heap Dump 的工具都可以獲取 Heap Dump 文件冈涧。
比如:jdk 自帶的工具 jvisualvm茂附。
其它工具:Eclipse memory analyzer(jmat)正蛙、JProfiler 等。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末营曼,一起剝皮案震驚了整個濱河市乒验,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蒂阱,老刑警劉巖锻全,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異录煤,居然都是意外死亡鳄厌,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進店門妈踊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來了嚎,“玉大人,你說我怎么就攤上這事响委⌒滤迹” “怎么了?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵赘风,是天一觀的道長。 經(jīng)常有香客問我纵刘,道長邀窃,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任假哎,我火速辦了婚禮瞬捕,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘舵抹。我一直安慰自己肪虎,他們只是感情好,可當我...
    茶點故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布惧蛹。 她就那樣靜靜地躺著扇救,像睡著了一般。 火紅的嫁衣襯著肌膚如雪香嗓。 梳的紋絲不亂的頭發(fā)上迅腔,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天,我揣著相機與錄音靠娱,去河邊找鬼沧烈。 笑死,一個胖子當著我的面吹牛像云,可吹牛的內(nèi)容都是我干的锌雀。 我是一名探鬼主播蚂夕,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼腋逆!你這毒婦竟也來了婿牍?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤闲礼,失蹤者是張志新(化名)和其女友劉穎牍汹,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體柬泽,經(jīng)...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡慎菲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了锨并。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片露该。...
    茶點故事閱讀 38,117評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖第煮,靈堂內(nèi)的尸體忽然破棺而出解幼,到底是詐尸還是另有隱情,我是刑警寧澤包警,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布撵摆,位于F島的核電站,受9級特大地震影響害晦,放射性物質(zhì)發(fā)生泄漏特铝。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一壹瘟、第九天 我趴在偏房一處隱蔽的房頂上張望鲫剿。 院中可真熱鬧,春花似錦稻轨、人聲如沸灵莲。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽政冻。三九已至,卻和暖如春粱挡,著一層夾襖步出監(jiān)牢的瞬間知纷,已是汗流浹背紊选。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人氓润。 一個月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子圾旨,可洞房花燭夜當晚...
    茶點故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內(nèi)容