{"id":1936,"date":"2019-09-16T08:01:13","date_gmt":"2019-09-16T00:01:13","guid":{"rendered":"https:\/\/coderbee.net\/?p=1936"},"modified":"2019-09-15T22:51:32","modified_gmt":"2019-09-15T14:51:32","slug":"jvm-%e5%a0%86%e5%a4%96%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f%e5%88%86%e6%9e%90%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/jvm\/20190916\/1936","title":{"rendered":"JVM \u5806\u5916\u5185\u5b58\u6cc4\u6f0f\u5206\u6790\uff08\u4e8c\uff09"},"content":{"rendered":"<p>\u5173\u4e8e \u5806\u5916\u5185\u5b58\u7684\u7ec4\u6210\u53ef\u4ee5\u770b\u4e0a\u4e00\u7bc7\u6587\u7ae0 <a href=\"https:\/\/coderbee.net\/?p=1936\">JVM \u5806\u5916\u5185\u5b58\u6cc4\u6f0f\u5206\u6790\uff08\u4e00\uff09<\/a><\/p>\n<h1>1. NMT<\/h1>\n<p>NMT\uff08Native Memory Tracking\uff09\u662f HotSpot JVM \u5f15\u5165\u7684\u8ddf\u8e2a JVM \u5185\u90e8\u4f7f\u7528\u7684\u672c\u5730\u5185\u5b58\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7 jcmd \u5de5\u5177\u8bbf\u95ee NMT \u6570\u636e\u3002NMT \u76ee\u524d\u4e0d\u652f\u6301\u8ddf\u8e2a\u7b2c\u4e09\u65b9\u672c\u5730\u4ee3\u7801\u7684\u5185\u5b58\u5206\u914d\u548c JDK \u7c7b\u5e93\u3002<\/p>\n<p>NMT \u4e0d\u8ddf\u8e2a\u975e JVM \u4ee3\u7801\u7684\u5185\u5b58\u5206\u914d\uff0c\u672c\u5730\u4ee3\u7801\u91cc\u7684\u5185\u5b58\u6cc4\u9732\u9700\u8981\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u652f\u6301\u7684\u5de5\u5177\u6765\u5b9a\u4f4d\u3002<\/p>\n<h2>1.1 \u5f00\u542f NMT<\/h2>\n<p>\u542f\u7528 NMT \u4f1a\u5e26\u6765 5-10% \u7684\u6027\u80fd\u635f\u5931\u3002NMT \u7684\u5185\u5b58\u4f7f\u7528\u7387\u60c5\u51b5\u9700\u8981\u6dfb\u52a0\u4e24\u4e2a\u673a\u5668\u5b57 word \u5230 malloc \u5185\u5b58\u7684 malloc \u5934\u91cc\u3002NMT \u5185\u5b58\u4f7f\u7528\u7387\u4e5f\u88ab NMT \u8ddf\u8e2a\u3002<\/p>\n<p>\u542f\u52a8\u547d\u4ee4\uff1a <code>-XX:NativeMemoryTracking=[off | summary | detail]<\/code>\u3002<\/p>\n<blockquote>\n<p>off\uff1aNMT \u9ed8\u8ba4\u662f\u5173\u95ed\u7684\uff1b<br \/>\n  summary\uff1a\u53ea\u6536\u96c6\u5b50\u7cfb\u7edf\u7684\u5185\u5b58\u4f7f\u7528\u7684\u603b\u8ba1\u6570\u636e\uff1b<br \/>\n  detail\uff1a\u6536\u96c6\u6bcf\u4e2a\u8c03\u7528\u70b9\u7684\u5185\u5b58\u4f7f\u7528\u6570\u636e\u3002<\/p>\n<\/blockquote>\n<h2>1.2 jcmd \u8bbf\u95ee NMT \u6570\u636e<\/h2>\n<p>\u547d\u4ee4\uff1a <code>jcmd &lt;pid&gt; VM.native_memory [summary | detail | baseline | summary.diff | detail.diff | shutdown] [scale= KB | MB | GB]<\/code><\/p>\n<table>\n<thead>\n<tr>\n<th>option<\/th>\n<th>desc<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>summary<\/td>\n<td>\u6309\u5206\u7c7b\u6253\u5370\u6c47\u603b\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>detail<\/td>\n<td>\u6309\u5206\u7c7b\u6253\u5370\u6c47\u603b\u6570\u636e<br \/>\u6253\u5370\u865a\u62df\u5185\u5b58\u6620\u5c04<br \/>\u6309\u8c03\u7528\u70b9\u6253\u5370\u5185\u5b58\u4f7f\u7528\u6c47\u603b<\/td>\n<\/tr>\n<tr>\n<td>baseling<\/td>\n<td>\u521b\u5efa\u5185\u5b58\u4f7f\u7528\u5feb\u7167\u7528\u4e8e\u540e\u7eed\u5bf9\u6bd4<\/td>\n<\/tr>\n<tr>\n<td>summary.diff<\/td>\n<td>\u57fa\u4e8e\u6700\u65b0\u7684\u57fa\u7ebf\u6253\u5370\u4e00\u4efd\u6c47\u603b\u62a5\u544a<\/td>\n<\/tr>\n<tr>\n<td>detail.diff<\/td>\n<td>\u57fa\u4e8e\u6700\u65b0\u7684\u57fa\u7ebf\u6253\u5370\u4e00\u4efd\u660e\u7ec6\u62a5\u544a<\/td>\n<\/tr>\n<tr>\n<td>shutdown<\/td>\n<td>\u5173\u95ed NMT<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728 NMT \u542f\u7528\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u884c\u9009\u9879\u5728 JVM \u9000\u51fa\u65f6\u8f93\u51fa\u6700\u540e\u7684\u5185\u5b58\u4f7f\u7528\u6570\u636e\uff1a<br \/>\n<code>-XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics<\/code><\/p>\n<p><!--more--><\/p>\n<h2>1.3 \u4f7f\u7528 NMT \u68c0\u6d4b\u5185\u5b58\u6cc4\u9732<\/h2>\n<ol>\n<li>\u5f00\u542f NMT\uff0c\u7528\u547d\u4ee4\uff1a <code>-XX:NativeMemoryTracking=summary|detail<\/code><\/li>\n<li>\u521b\u5efa\u57fa\u7ebf\uff0c\u7528\u547d\u4ee4\uff1a <code>jcmd &lt;pid&gt; VM.native_memory baseline<\/code><\/li>\n<li>\u89c2\u5bdf\u5185\u5b58\u53d8\u5316\uff1a <code>jcmd &lt;pid&gt; VM.native_memory detail.diff<\/code><\/li>\n<\/ol>\n<p>NMT \u6570\u636e\u8f93\u51fa\u89e3\u91ca\uff1a<\/p>\n<p>reserved memory\uff1a\u9884\u8ba2\u5185\u5b58\uff0c\u4e0d\u8868\u793a\u5b9e\u9645\u4f7f\u7528\uff0c\u6700\u4e3b\u8981\u7684\u662f\u7533\u8bf7\u4e86\u4e00\u6279\u8fde\u7eed\u7684\u5730\u5740\u7a7a\u95f4\uff1b\uff08OS \u89d2\u5ea6\uff09<br \/>\ncommited memory\uff1a\u5b9e\u9645\u4f7f\u7528\u7684\u3002\uff08OS \u89d2\u5ea6\uff09<br \/>\n\u5bf9\u4e8e 64 \u4f4d\u7684\u7cfb\u7edf\uff0c\u5730\u5740\u7a7a\u95f4\u51e0\u4e4e\u662f\u65e0\u9650\u7684\uff0c\u4f46\u8d8a\u6765\u8d8a\u591a\u7684\u5185\u5b58 committed\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4 swapping \u6216\u672c\u5730 OOM \u3002<\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6765\u81ea <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/guides\/troubleshoot\/tooldescr007.html\">https:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/guides\/troubleshoot\/tooldescr007.html<\/a> \u3002<\/p>\n<p><code>-XX:NativeMemoryTracking=summary<\/code> \u4e0e <code>jcmd &lt;pid&gt; VM.native_memory summary<\/code>  \u8f93\u51fa\uff1a<\/p>\n<pre><code>Total:  reserved=664192KB,  committed=253120KB  &lt;--- total memory tracked by Native Memory Tracking\n\n-     Java Heap (reserved=516096KB, committed=204800KB)  &lt;--- Java Heap\n                (mmap: reserved=516096KB, committed=204800KB)\n\n-     Class (reserved=6568KB, committed=4140KB)     &lt;--- class metadata\n            (classes #665)                          &lt;--- number of loaded classes\n            (malloc=424KB, #1000)                   &lt;--- malloc'd memory, #number of malloc\n            (mmap: reserved=6144KB, committed=3716KB)\n\n-     Thread (reserved=6868KB, committed=6868KB)\n            (thread #15)                            &lt;--- number of threads\n            (stack: reserved=6780KB, committed=6780KB) &lt;--- memory used by thread stacks\n            (malloc=27KB, #66)\n            (arena=61KB, #30)                       &lt;--- resource and handle areas\n\n-     Code (reserved=102414KB, committed=6314KB)\n           (malloc=2574KB, #74316)\n           (mmap: reserved=99840KB, committed=3740KB)\n\n-     GC (reserved=26154KB, committed=24938KB)\n           (malloc=486KB, #110)\n           (mmap: reserved=25668KB, committed=24452KB)\n\n-     Compiler (reserved=106KB, committed=106KB)\n               (malloc=7KB, #90)\n               (arena=99KB, #3)\n\n-     Internal (reserved=586KB, committed=554KB)\n               (malloc=554KB, #1677)\n               (mmap: reserved=32KB, committed=0KB)\n\n-     Symbol (reserved=906KB, committed=906KB)\n             (malloc=514KB, #2736)\n             (arena=392KB, #1)\n\n-     Memory Tracking (reserved=3184KB, committed=3184KB)\n                      (malloc=3184KB, #300)\n\n-     Pooled Free Chunks (reserved=1276KB, committed=1276KB)\n                         (malloc=1276KB)\n\n-     Unknown (reserved=33KB, committed=33KB)\n              (arena=33KB, #1)\n<\/code><\/pre>\n<p><code>-XX:NativeMemoryTracking=detail<\/code> \u4e0e <code>jcmd &lt;pid&gt; VM.native_memory detail<\/code> \u7ec4\u5408\u7684\u8f93\u51fa\u793a\u4f8b\uff1a<\/p>\n<h1>2. \u7cfb\u7edf\u5c42\u9762\u7684\u5206\u6790\u601d\u8def<\/h1>\n<p>\u5185\u5b58\u6cc4\u6f0f\u4e00\u822c\u90fd\u4e0d\u662f\u7a81\u7136\u731b\u589e\u5230\u6781\u9650\uff0c\u800c\u662f\u4e00\u4e2a\u6162\u6162\u589e\u957f\u7684\u8fc7\u7a0b\uff0c\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u9009\u53d6\u4e24\u4e2a\u65f6\u95f4\u7684\u5185\u5b58\u6765\u8fdb\u884c\u5bf9\u6bd4\uff0c\u770b\u65b0\u589e\u7684\u5185\u5b58\u91cc\u5230\u5e95\u5b58\u7684\u662f\u4ec0\u4e48\u5185\u5bb9\u3002<\/p>\n<h2>2.0 gdb \u65b9\u5f0f<\/h2>\n<p>gdb \u5bfc\u51fa\u6307\u5b9a\u5730\u5740\u8303\u56f4\u7684\u5185\u5b58\u5757\u7684\u5185\u5bb9 \uff1a<\/p>\n<pre><code>sudo gdb --batch --pid 2754 -ex \"dump memory a.dump 0x7f1023ff6000 0x7f1023ff6000+268435456\"\n<\/code><\/pre>\n<p>\u7136\u540e\u7528 <code>hexdump -C \/tmp\/memory.bin<\/code> \u6216 <code>strings \/tmp\/memory.bin |less<\/code> \u67e5\u770b\u5185\u5b58\u5757\u91cc\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u5982\u679c\u5185\u5b58\u5757\u91cc\u5b58\u7684\u662f\u6587\u672c\u4fe1\u606f\uff0c\u8fd9\u6837\u662f\u53ef\u4ee5\u770b\u51fa\u5b58\u7684\u662f\u4ec0\u4e48\u5185\u5bb9\u7684\uff0c\u5982\u679c\u662f\u4e8c\u8fdb\u5236\u7684\u5185\u5b58\uff0c\u5c31\u6ca1\u6cd5\u770b\u4e86\u3002<\/p>\n<h2>2.1 jstack\/jmap + core dump<\/h2>\n<p>\u5148\u751f\u6210 core dump\uff0c\u7136\u540e\u4ece core dump \u91cc\u63d0\u53d6\u7ebf\u7a0b\u6808\u3001JVM \u5806 dump\uff0cJDK 8 \u4e0b\u63d0\u53d6\u6210\u529f\uff1a<\/p>\n<pre><code># \u4f7f\u7528 gcore \u547d\u4ee4\u751f\u6210 core dump\uff0c\ngcore 1791\n\n# \u4f7f\u7528 jstack \u4ece core dump \u6587\u4ef6\u63d0\u53d6\u7ebf\u7a0b\u4fe1\u606f\n~\/zuul-jdk\/zulu8.40.0.25-ca-jdk8.0.222-linux_x64\/bin\/jstack ~\/zuul-jdk\/zulu8.40.0.25-ca-jdk8.0.222-linux_x64\/bin\/java core.1791\n\n# \u4f7f\u7528 jmap \u4ece core dump \u6587\u4ef6\u63d0\u53d6 JVM \u5806 dump\n~\/zuul-jdk\/zulu8.40.0.25-ca-jdk8.0.222-linux_x64\/bin\/jmap -dump:format=b,file=zuul.jmap.hprof ~\/zuul-jdk\/zulu8.40.0.25-ca-jdk8.0.222-linux_x64\/bin\/java core.1791\n\n# jstack\u3001jmap \u4ece core dump \u91cc\u63d0\u53d6\u4fe1\u606f\u7684\u65b9\u5f0f\uff0cexec \u4e00\u822c\u662f\u6307\u5411\u53ef\u6267\u884c\u547d\u4ee4 java \u7684\u8def\u5f84\njstack exec core-file\njmap &lt;options&gt; exec core-file\n<\/code><\/pre>\n<h2>2.2 jhsdb<\/h2>\n<p>jhsdb\uff1a hsdb \u662f HotSpot debugger \u7684\u7b80\u79f0\uff0c\u662f JDK9 \u5f00\u59cb\u5f15\u5165\u7684\u4e00\u4e2a\u8c03\u8bd5\u5de5\u5177\u3002<\/p>\n<pre><code>$ jhsdb\n    clhsdb              command line debugger\n    hsdb                ui debugger\n    debugd --help       to get more information\n    jstack --help       to get more information\n    jmap   --help       to get more information\n    jinfo  --help       to get more information\n    jsnap  --help       to get more information\n<\/code><\/pre>\n<p>\u5728 openJDK 11 \u63d0\u53d6\u5b9e\u64cd\u5931\u8d25\u4e86\uff0c\u751f\u6210\u5806 dump \u65f6\u4f1a\u51fa\u73b0\u4e00\u4e9b\u5185\u5b58\u5730\u5740\u8bfb\u53d6\u5931\u8d25\u3002<\/p>\n<p>\u7528 jstack \u4ece core dump \u63d0\u53d6\u4fe1\u606f:<\/p>\n<pre><code>sudo jstack -J-d64 \/usr\/bin\/java core.2316\n\njhsdb jstack --exe \/usr\/bin\/java --core core.2316\n<\/code><\/pre>\n<p><code>-d64<\/code> \u8868\u793a64\u4f4d\u7684\u7cfb\u7edf\uff0c\u8fd9\u4e24\u4e2a\u4e5f\u662f\u7f51\u4e0a\u627e\u7684\uff0c\u6ca1\u6709\u5b9e\u9645\u6210\u529f\u3002<\/p>\n<h1>3. \u53c2\u8003\u8d44\u6599<\/h1>\n<ul>\n<li><a href=\"https:\/\/coldwalker.com\/2018\/08\/\/troubleshooter_native_memory_increase\/\">Java\u5806\u5916\u5185\u5b58\u589e\u957f\u95ee\u9898\u6392\u67e5Case<\/a><\/li>\n<li><a href=\"http:\/\/blog.2baxb.me\/archives\/918\">\u8bb0\u4e00\u6b21java native memory\u589e\u957f\u95ee\u9898\u7684\u6392\u67e5<\/a><\/li>\n<li><a href=\"https:\/\/www.atlassian.com\/blog\/archives\/so-you-want-your-jvms-heap\">\u4ece core dump \u63d0\u53d6jvm\u4fe1\u606f\u7684\u6587\u7ae0, so-you-want-your-jvms-heap<\/a><\/li>\n<\/ul>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e \u5806\u5916\u5185\u5b58\u7684\u7ec4\u6210\u53ef\u4ee5\u770b\u4e0a\u4e00\u7bc7\u6587\u7ae0 JVM \u5806\u5916\u5185\u5b58\u6cc4\u6f0f\u5206\u6790\uff08\u4e00\uff09 1. NM &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/jvm\/20190916\/1936\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[96],"tags":[233,322],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1936"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1936"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1936\/revisions"}],"predecessor-version":[{"id":1939,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1936\/revisions\/1939"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}