Java如何生成Heap Dump及OOM问题排查

Heap Dump简述

Heap dump文件是一个二进制文件,它保存了某一时刻JVM堆中对象使用情况。Heap Dump文件是指定时刻的Java堆栈的快照,是一种镜像文件。Heap Dump一般都包含了一个堆中的Java Objects, Class等基本信息。同时,当你在执行一个转储操作时,往往会触发一次GC,所以你转储得到的文件里包含的信息通常是有效的内容(包含比较少,或没有垃圾对象了) 。我们可以这么理解:heap dump记录内存信息的,thread dump是记录CPU信息的。

Heap Dump 包含的信息

  • 所有的对象信息
    对象的类信息、字段信息、原生值(int, long等)及引用值
  • 所有的类信息
    类加载器、类名、超类及静态字段
  • 垃圾回收的根对象
    根对象是指那些可以直接被虚拟机触及的对象
  • 线程栈及局部变量
    包含了转储时刻的线程调用栈信息和栈帧中的局部变量信息

Heap Dump 获取方式

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

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

eg. 1) 在windows主机上面通过tasklist获取进程pid

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

映像名称                       PID 会话名              会话#       内存使用 
========================= ======== ================ =========== ============
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

    如上图所示,红色圈出来的即为占用内存最多的对象,也是最可能发生OOM问题的地方;

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

jcmd GC.heap_dump c:\dump\heap.hprof

3. 使用 JVM 参数获取 dump 文件

1. -XX:+HeapDumpOnOutOfMemoryError
当OutOfMemoryError发生时自动生成 Heap Dump 文件。
这可是一个非常有用的参数,因为当你需要分析Java内存使用情况时,往往是在OOM(OutOfMemoryError)发生时。

eg.


image.png

image.png

image.png
  1. -XX:+HeapDumpBeforeFullGC
    当 JVM 执行 FullGC 前执行 dump。
  1. -XX:+HeapDumpAfterFullGC
    当 JVM 执行 FullGC 后执行 dump。
  1. -XX:+HeapDumpOnCtrlBreak
    交互式获取dump。在控制台按下快捷键Ctrl + Break时,JVM就会转存一下堆快照。
  1. -XX:HeapDumpPath=c:\test.hprof
    指定 dump 文件存储路径。

注意:JVM 生成 Heap Dump 的时候,虚拟机是暂停一切服务的。如果是线上系统执行 Heap Dump 时需要注意。

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

分析 Heap Dump 的工具都可以获取 Heap Dump 文件。
比如:jdk 自带的工具 jvisualvm。
其它工具:Eclipse memory analyzer(jmat)、JProfiler 等。

你可能感兴趣的:(Java如何生成Heap Dump及OOM问题排查)