{"id":328165,"date":"2021-02-07T20:39:09","date_gmt":"2021-02-07T12:39:09","guid":{"rendered":"http:\/\/4563.org\/?p=328165"},"modified":"2021-02-07T20:39:09","modified_gmt":"2021-02-07T12:39:09","slug":"java-%e8%bf%9b%e7%a8%8b-hang-%e4%bd%8f%e4%ba%86-%e6%80%8e%e4%b9%88%e6%8b%bf%e5%88%b0-heap-dump","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=328165","title":{"rendered":"Java \u8fdb\u7a0b hang \u4f4f\u4e86, \u600e\u4e48\u62ff\u5230 heap dump?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Java \u8fdb\u7a0b hang \u4f4f\u4e86, \u600e\u4e48\u62ff\u5230 heap dump?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : manecocomph <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u539f\u6587\u683c\u5f0f\u7a0d\u597d , \u6b22\u8fce\u8bc4\u8bba\u8ba8\u8bba, \u8ba8\u8bba\u80fd\u5b66\u5230\u66f4\u591a\u77e5\u8bc6. <\/p>\n<p>\u539f\u6587\u94fe\u63a5: https:\/\/mp.weixin.qq.com\/s\/7SHlfonUEW8oWx_IAjuYRw<\/p>\n<p>\u7ecf\u5e38\u8bca\u65ad Java \u95ee\u9898, \u505a heap dump \u662f\u5e38\u89c4\u9879\u76ee, \u56e0\u4e3a heap dump \u5c31\u50cf\u4e00\u4e2a\u5b9d\u85cf\u4e00\u6837, \u91cc\u9762\u662f\u8fd0\u884c\u65f6\u7684\u5404\u79cd\u771f\u5b9e\u72b6\u6001. \u4e00\u822c\u4f7f\u7528 JDK \u63d0\u4f9b\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6216\u8005\u4f7f\u7528\u5c01\u88c5\u597d\u7684\u7279\u5b9a\u7f16\u7a0b\u63a5\u53e3, \u5f88\u5bb9\u6613\u505a\u51fa\u4e00\u4e2a heap dump. \u4f46\u662f\u5982\u679c\u9047\u5230 JVM \u5df2\u7ecf\u4e0d\u54cd\u5e94\u4e86, \u5982\u4f55\u624d\u80fd\u505a\u51fa\u4e00\u4e2a heap dump \u5462?<\/p>\n<h2>\u573a\u666f\u91cd\u73b0<\/h2>\n<p>\u770b\u5230\u7ebf\u4e0a\u6709\u4e2a\u5e94\u7528 CPU \u4f7f\u7528\u7387\u8fbe\u5230 100%, \u4e0d\u54cd\u5e94\u4efb\u4f55\u8bf7\u6c42, \u8fdb\u4e00\u6b65\u68c0\u67e5\u53d1\u73b0\u662f GC \u5bfc\u81f4\u7684, GC overhead \u5df2\u7ecf\u8fbe\u5230 100%. \u8fd9\u4e2a\u65f6\u5019, \u4e3a\u4e86\u53d1\u73b0\u5185\u5b58\u5806\u79ef\u7684\u771f\u6b63\u539f\u56e0, \u9700\u8981\u505a\u4e00\u4e2a heap dump. <\/p>\n<p>\u9996\u5148\u5c1d\u8bd5\u4f7f\u7528\u5e94\u7528\u6846\u67b6\u63d0\u4f9b\u7684 Restful API \u7684\u5f62\u5f0f\u53bb\u505a heap dump, \u53ef\u662f\u7b49\u4e86 10 \u79d2\u540e\u8fd8\u6ca1\u54cd\u5e94. \u8fd9\u662f\u56e0\u4e3a: \u5185\u5b58\u5df2\u7ecf\u4e0d\u591f\u7528, \u4f46\u662f\u4e4b\u524d\u8fdb\u6765\u7684\u8bf7\u6c42\u8fd8\u6ca1\u5b8c\u6210, \u5404\u4e2a\u7ebf\u7a0b\u90fd\u5728\u7b49\u5f85\u5185\u5b58\u7ed9\u81ea\u5df1\u7528, \u53ef\u662f\u5185\u5b58\u5374\u4e0d\u80fd\u91ca\u653e\u51fa\u6765. \u5f62\u6210\u4e00\u4e2a\u77db\u76fe\u4f53: \u5e94\u7528\u7ebf\u7a0b\u7b49\u7a7a\u95f2\u5185\u5b58\u7ee7\u7eed\u8fd0\u884c, \u5374\u6ca1\u6709\u5185\u5b58, \u53ea\u80fd\u6682\u505c, GC \u7ebf\u7a0b\u62fc\u547d\u505a Full GC, \u53ef\u662f\u6ca1\u6709\u592a\u591a\u5185\u5b58\u53ef\u4ee5\u91ca\u653e, \u53ea\u80fd\u4e00\u904d\u4e00\u904d\u505a. <\/p>\n<p>Hotspot JVM \u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\u7684\u9ed8\u8ba4\u5904\u7406\u662f: \u5982\u679c\u8fde\u7eed 5 \u6b21 Full GC, \u6bcf\u6b21 GC overhead \u90fd\u8d85\u8fc7 98%, \u56de\u6536\u7684\u7a7a\u95f2\u5185\u5b58\u4e0d\u8db3 2%, \u5c31\u629b\u51fa OOM. \u4e0d\u6ee1\u8db3\u4efb\u4f55\u4e00\u4e2a\u6761\u4ef6, \u6bd4\u5982\u6709\u6b21\u56de\u6536\u591a\u4e8e 2%, \u5c31\u4e0d\u4f1a\u629b\u51fa OOM. <\/p>\n<p>\u8fd9\u4e2a\u65f6\u5019, \u7531\u4e8e\u6240\u6709\u5e94\u7528\u7ebf\u7a0b\u90fd\u5b8c\u6210\u4e0d\u4e86\u73b0\u6709\u7684\u4efb\u52a1, \u6240\u4ee5\u65e0\u6cd5\u627f\u63a5\u65b0\u7684\u8bf7\u6c42, \u5bfc\u81f4\u4e0d\u54cd\u5e94\u4efb\u4f55\u65b0\u8bf7\u6c42. <\/p>\n<h2>JDK \u63d0\u4f9b\u7684\u547d\u4ee4\u884c jcmd | jmap<\/h2>\n<p>\u65e9\u671f JDK \u63d0\u4f9b\u4e86 jmap \u547d\u4ee4\u884c\u5de5\u5177, \u540e\u7eed\u7684 JDK \u53c8\u63d0\u4f9b\u4e86 jcmd \u547d\u4ee4\u884c\u5de5\u5177, \u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u65b9\u4fbf\u7684\u53bb\u505a heap dump. \u4e8e\u662f, \u6211\u4eec\u8f6c\u800c\u4f7f\u7528 jcmd \u53bb\u5c1d\u8bd5\u83b7\u5f97 heap dump. <\/p>\n<p>\u7136\u800c, \u8fd9\u6b21\u53c8\u5931\u8d25\u4e86: \u65e0\u6cd5 attach \u5230\u76ee\u6807\u8fdb\u7a0b: <\/p>\n<p>\/usr\/bin\/jcmd 7674 GC.heap_dump \/tmp\/heap.log.hprof 7674: com.sun.tools.attach.AttachNotSupportedException:Unable to open socket file:target process not responding or HotSpot VM not loaded at sun.tools.attach.LinuxVirtualMachine.&lt;init&gt;(LinuxVirtualMachine.java:106) at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:63) at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:213) at sun.tools.jcmd.JCmd.executeCommandForPid(JCmd.java:140) at sun.tools.jcmd.JCmd.main(JCmd.java:129)<\/p>\n<p>\u5076\u5c14\u60c5\u51b5\u4e0b, \u5982\u679c\u6211\u4eec\u591a\u8bd5\u51e0\u6b21, \u6709\u53ef\u80fd\u67d0\u6b21\u6b63\u597d\u78b0\u5230 GC \u7ebf\u7a0b\u91ca\u653e\u51fa\u90e8\u5206\u5185\u5b58, \u8ba9 JVM \u53ef\u4ee5\u54cd\u5e94\u6211\u4eec\u7684\u8bf7\u6c42. \u8fd9\u79cd\u6982\u7387\u975e\u5e38\u5c0f. <\/p>\n<p>\u90a3\u4e48\u6709\u6ca1\u6709\u5176\u5b83\u6bd4\u8f83\u9760\u8c31\u7684\u529e\u6cd5\u5462?<\/p>\n<h2>Core dump \u8f6c heap dump<\/h2>\n<p>core dump \u662f\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u7a0b\u7684\u77ac\u65f6\u7684 snapshot. \u6211\u4eec\u53ef\u4ee5\u5148\u505a\u4e00\u4e2a core dump, \u7136\u540e\u901a\u8fc7 core dump \u8f6c heap dump \u7684\u65b9\u5f0f, \u83b7\u5f97\u6211\u4eec\u671f\u671b\u7684 heap dump. \u8be6\u7ec6\u6b65\u9aa4\u5982\u4e0b: <\/p>\n<ol>\n<li>\n<p>\u9996\u5148 \u5b89\u88c5 gcore $ sudo apt install gdb -y #gcore command in gdb package<\/p>\n<\/li>\n<li>\n<p>\u628a core size limit \u6539\u4e3a unlimited $ sudo cp \/etc\/security\/limits.conf \/etc\/security\/limits.d\/ # \u590d\u5236 limits.conf \u6a21\u677f\u6587\u4ef6\u5230\u914d\u7f6e\u6587\u4ef6\u5939 $ sudo vim \/etc\/security\/limits.d\/limits.conf # \u7f16\u8f91\u914d\u7f6e\u6587\u4ef6, \u4fee\u6539\u6211\u4eec\u76ee\u6807 Java \u8fdb\u7a0b\u7684\u7528\u6237\u7684\u914d\u7f6e appUser hard core unlimited appUser soft core unlimited<\/p>\n<\/li>\n<li>\n<p>\u83b7\u53d6 core dump<\/p>\n<p>$ pgrep java # \u663e\u793a\u67e5\u770b\u76ee\u6807\u8fdb\u7a0b\u7684 pid $ sudo gcore &lt;pid&gt;<br \/> $ ls -lah core.&lt;pid&gt; # \u67e5\u770b\u83b7\u53d6\u7684 core dump \u7684\u5927\u5c0f, \u901a\u5e38\u5728\u5f53\u524d\u76ee\u5f55<\/p>\n<\/li>\n<li>\n<p>\u628a core file \u7684 hard, soft \u4fee\u6539\u7684\u914d\u7f6e\u6539\u56de\u6765 $ sudo rm -f \/etc\/security\/limits.d\/limits.conf # \u4e4b\u524d\u6211\u4eec\u628a\u6a21\u677f\u52a0\u5230\u8fd9\u4e2a\u6587\u4ef6, \u73b0\u5728\u5220\u6389<\/p>\n<\/li>\n<li>\n<p>\u8f6c\u6362 core dump \u5230 Java heap dump. \u4e00\u5b9a\u8981\u4f7f\u7528\u8fd0\u884c\u5bf9\u5e94\u76ee\u6807\u8fdb\u7a0b\u8fd0\u884c\u7684 JDK.<\/p>\n<p>$ find \/app -name jmap # \u627e\u5230\u8fd0\u884c\u76ee\u6807\u8fdb\u7a0b\u7684 JDK \u91cc\u9762\u7684 jmap \u547d\u4ee4 $ sudo \/usr\/bin\/jmap -dump:format=b,file=heap.hprof \/usr\/bin\/java .\/core.&lt;pid&gt; #\u8f6c\u6362<\/p>\n<\/li>\n<li>\n<p>\u7b49\u4f7f\u7528\u5b8c\u4e4b\u540e, \u6e05\u7406 dump \u6587\u4ef6(\u56e0\u4e3a dump \u6587\u4ef6\u6bd4\u8f83\u5360\u78c1\u76d8)<\/p>\n<p>$ rm core.&lt;pid&gt; dump.hprof<\/p>\n<\/li>\n<\/ol>\n<p>\u81f3\u6b64, \u6211\u4eec\u7ec8\u4e8e\u6709\u4e86\u8fd9\u4e2a heap dump \u5b9d\u85cf, \u5269\u4e0b\u7684\u5c31\u662f\u53bb\u5b9d\u85cf\u91cc\u9762\u67e5\u627e\u6211\u4eec\u9700\u8981\u7684\u4fe1\u606f\u4e86(\u540e\u7eed\u5c06\u4f1a\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 mat \u6709\u6548\u7684\u5206\u6790 heap dump).<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Java \u8fdb\u7a0b hang \u4f4f\u4e86, &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/328165"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=328165"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/328165\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=328165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=328165"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=328165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}