{"id":1929,"date":"2019-09-13T08:19:10","date_gmt":"2019-09-13T00:19:10","guid":{"rendered":"https:\/\/coderbee.net\/?p=1929"},"modified":"2019-09-13T08:19:10","modified_gmt":"2019-09-13T00:19:10","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%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/jvm\/20190913\/1929","title":{"rendered":"JVM \u5806\u5916\u5185\u5b58\u6cc4\u6f0f\u5206\u6790\uff08\u4e00\uff09"},"content":{"rendered":"<h1>1. JVM \u611f\u77e5\u5bb9\u5668\u8d44\u6e90<\/h1>\n<p>Java \u5e94\u7528\u90e8\u7f72\u5728 Kubernetes \u96c6\u7fa4\u91cc\uff0c\u6bcf\u4e2a\u5bb9\u5668\u53ea\u8fd0\u884c\u4e00\u4e2a\u8fdb\u7a0b\uff0c JVM \u7684\u542f\u52a8\u547d\u4ee4\u662f\u6253\u5305\u5728\u955c\u50cf\u6587\u4ef6\u91cc\u7684\u3002<\/p>\n<p>\u5e38\u89c4\u7684\u65b9\u5f0f\u662f\u91c7\u7528 <code>-Xmx4g -Xms2g<\/code> \u8fd9\u6837\u7684\u53c2\u6570\u6765\u6307\u5b9a JVM \u5806\u7684\u6700\u5927\u3001\u6700\u5c0f\u5c3a\u5bf8\uff0c\u5982\u679c\u9700\u8981\u8c03\u6574\u5806\u5927\u5c0f\u5c31\u9700\u8981\u91cd\u65b0\u6253\u5305\u955c\u50cf\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u56e0\u4e3a\u4fee\u6539\u5806\u5927\u5c0f\u800c\u91cd\u65b0\u6253\u5305\uff0c\u4ece JDK 8u191 \u7248\u672c\u5f00\u59cb\u652f\u6301 JVM \u611f\u77e5\u5bb9\u5668\u8d44\u6e90\u9650\u5236\uff0c\u8fd9\u6837\u5728\u8c03\u6574 JVM \u5185\u5b58\u5206\u914d\u65f6\u5c31\u4e0d\u9700\u8981\u91cd\u65b0\u6253\u5305\u955c\u50cf\u6587\u4ef6\uff0c\u91c7\u7528\u4e0b\u9762\u7684\u53c2\u6570\u6765\u4f7f JVM \u5728\u542f\u52a8\u65f6\u611f\u77e5\u5230\u5bb9\u5668\u7684\u8d44\u6e90\u9650\u5236\uff0c\u5e76\u8bbe\u5b9a\u5806\u7684\u5927\u5c0f\uff1a<\/p>\n<pre><code>-XX:+UseCGroupMemoryLimitForHeap\n-XX:InitialRAMPercentage=60.00\n-XX:MaxRAMPercentage=80.00\n-XX:MinRAMPercentage=60.00 \n<\/code><\/pre>\n<p>\u5047\u5982\u5206\u914d\u7ed9\u5bb9\u5668\u7684\u5185\u5b58\u4e0a\u9650\u662f 4G\uff0c\u90a3\u4e48\u4e0a\u8ff0\u914d\u7f6e\uff0cJVM \u5806\u7684\u521d\u59cb\u5927\u5c0f\u548c\u6700\u5c0f\u5c3a\u5bf8\u662f <code>4G * 0.6<\/code> \u5373 2.4G\uff0c\u6700\u5927\u5c3a\u5bf8\u662f <code>4G * 0.8<\/code> \u5373 3.2G\u3002<\/p>\n<h1>2. JVM \u88ab oomkill<\/h1>\n<p>\u4e0a\u9762\u7684\u914d\u7f6e\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\u53d1\u73b0\u5bb9\u5668\u81ea\u52a8\u91cd\u542f\u4e86\uff0c\u5728 linux \u4e0b\u901a\u8fc7 <code>dmesg<\/code> \u547d\u4ee4\u67e5\u770b\u7cfb\u7edf\u65e5\u5fd7\uff0c\u53ef\u4ee5\u770b\u5230\u7c7b\u4f3c\u4e0b\u9762\u7684\u65e5\u5fd7\uff1a<\/p>\n<pre><code>Aug  8 15:32:40 H-LDOCKER-01 kernel: [ pid ]   uid   tgid   total_vm      rss        nr_ptes   nr_pmds   swapents   oom_score_adj  name\nAug  8 15:32:40 H-LDOCKER-01 kernel: [33775]   1001  33775  7624373       2036828    4476      32        0          -998           java\nAug  8 15:32:40 H-LDOCKER-01 kernel: Memory cgroup out of memory: Kill process 33775 (java) score 0 or sacrifice child\nAug  8 15:32:40 H-LDOCKER-01 kernel: Killed process 33775 (java) total-vm:30497492kB, anon-rss:8134056kB, file-rss:13256kB\n<\/code><\/pre>\n<p><!--more--><\/p>\n<p>\u6ce8\u610f\uff1a\u4e0a\u9762\u65e5\u5fd7 rss \u5217\u8868\u793a\u8fdb\u7a0b\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f\uff0c\u5bf9\u5e94\u7684\u503c\u662f 2036828\uff0c\u5355\u4f4d\u662f 4KB\uff0c\u4e5f\u5373\u8fd9\u4e2a Java \u8fdb\u7a0b\u5360\u7528\u4e86 7.77G\uff0c\u5bb9\u5668\u5206\u914d\u7684\u5185\u5b58\u4e0a\u9650\u662f 8G\u3002\u7b2c3\u30014\u884c\u8868\u793a Java \u8fdb\u7a0b\u88ab oom_killer \u4e86\u3002<\/p>\n<blockquote>\n<p>OOM_killer \u662f Linux \u7684\u4e00\u79cd\u81ea\u6211\u4fdd\u62a4\u63aa\u65bd\uff0c\u5f53\u7cfb\u7edf\u5185\u5b58\u4e0d\u8db3\u65f6\u4e3a\u9632\u6b62\u51fa\u73b0\u4e25\u91cd\u95ee\u9898\uff0c\u7cfb\u7edf\u5524\u9192 oom_killer\uff0c\u6311\u51fa <code>\/proc\/&lt;pid&gt;\/oom_score<\/code> \u503c\u6700\u5927\u7684\u8fdb\u7a0b\u5e76 kill\u3002<\/p>\n<\/blockquote>\n<p>\u56e0\u4e3a\u5e94\u7528\u4e5f\u8f93\u51fa\u4e86 GC \u65e5\u5fd7\uff0c\u4ece\u8fdb\u7a0b\u88ab kill \u524d\u7684\u90a3\u4e2a\u65f6\u95f4\u8282\u70b9\u7684\u65e5\u5fd7\u6765\u770b\uff0cJVM \u7684\u5806\u662f\u8fdc\u8fdc\u6ca1\u6709 7G \u90a3\u4e48\u5927\u7684\uff0c\u591a\u51fa\u6765\u7684\u5176\u5b9e\u662f\u5806\u5916\u5185\u5b58\u3002<\/p>\n<h1>3. JVM \u5806\u5916\u5185\u5b58<\/h1>\n<p>JVM \u7684\u5806\u5916\u5185\u5b58\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>JVM \u81ea\u8eab\u8fd0\u884c\u5360\u7528\u7684\u7a7a\u95f4\uff1b<\/li>\n<li>\u7ebf\u7a0b\u6808\u5206\u914d\u5360\u7528\u7684\u7cfb\u7edf\u5185\u5b58\uff1b<\/li>\n<li>DirectByteBuffer \u5360\u7528\u7684\u5185\u5b58\uff1b<\/li>\n<li>JNI \u91cc\u5206\u914d\u7684\u5185\u5b58\uff1b<\/li>\n<li>Java 8 \u5f00\u59cb\u7684\u5143\u6570\u636e\u7a7a\u95f4\uff1b<\/li>\n<li>NIO \u7f13\u5b58<\/li>\n<li>Unsafe \u8c03\u7528\u5206\u914d\u7684\u5185\u5b58\uff1b<\/li>\n<li>codecache<\/li>\n<\/ul>\n<blockquote>\n<p>\u51b0\u5c71\u5bf9\u8c61\uff1a\u51b0\u5c71\u5bf9\u8c61\u662f\u6307\u5728 JVM \u5806\u91cc\u5360\u7528\u7684\u5185\u5b58\u5f88\u5c0f\uff0c\u4f46\u5176\u5b9e\u5f15\u7528\u4e86\u4e00\u5757\u5f88\u5927\u7684\u672c\u5730\u5185\u5b58\u3002DirectByteBuffer \u548c \u7ebf\u7a0b\u90fd\u5c5e\u4e8e\u8fd9\u7c7b\u5bf9\u8c61\u3002<\/p>\n<\/blockquote>\n<p>\u5806\u5916\u5185\u5b58\u6cc4\u6f0f\u4e00\u822c\u5f88\u96be\u901a\u8fc7 MAT \u4e4b\u7c7b\u7684\u5de5\u5177\u6765\u5206\u6790\uff0c\u5fc5\u987b\u901a\u8fc7\u64cd\u4f5c\u7cfb\u7edf\u5c42\u9762\u7684\u5de5\u5177\u6765\u3002<\/p>\n<p>\u5173\u4e8e\u672c\u5730\u5185\u5b58\u7684\u53ef\u4ee5\u53c2\u8003 IBM \u7684\u4e00\u4e2a\u5206\u4eab \u300aWhere Does All The Native Memory Go\u300b\uff0c\u53ef\u4ee5\u7f51\u4e0a\u627e\u4e0b\u8fd9\u4e2a PPT\uff0c\u4e0b\u9762\u662f\u5176\u4e2d\u7684\u4e00\u90e8\u5206\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"952\" height=\"6460\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/09\/where-does-all-native-memory-go-2.png\" alt=\"where-does-all-native-memory-go-2\" class=\"alignnone size-full wp-image-1931\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"798\" height=\"387\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/09\/where-does-all-native-memory-go-3.png\" alt=\"where-does-all-native-memory-go-3\" class=\"alignnone size-full wp-image-1932\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"937\" height=\"464\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/09\/where-does-all-native-memory-go-5.png\" alt=\"where-does-all-native-memory-go-5\" class=\"alignnone size-full wp-image-1933\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"393\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/09\/where-does-all-native-memory-go-6.png\" alt=\"where-does-all-native-memory-go-6\" class=\"alignnone size-full wp-image-1934\" \/><\/p>\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>1. JVM \u611f\u77e5\u5bb9\u5668\u8d44\u6e90 Java \u5e94\u7528\u90e8\u7f72\u5728 Kubernetes \u96c6\u7fa4\u91cc\uff0c &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/jvm\/20190913\/1929\">\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,321,322],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1929"}],"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=1929"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1929\/revisions"}],"predecessor-version":[{"id":1935,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1929\/revisions\/1935"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}