JVM-GC总结-配置实战--第三发

配置举例1:

vm args: 
-Xms60m -Xmx60m -XX:NewRatio=2 -XX:SurvivorRatio=3 -XX:MaxPermSize=30m -XX:MaxTenuringThreshold=3 -XX:+PrintHeapAtGC

使用工具:jvisualvm (安装visual gc 插件) 
这里写图片描述
根据设置堆内存为60m, 因为设置了-XX:NewRatio=2所以年轻代与年老代的比例是1:2, 
年轻代 20m,年老代40m, 设置-XX:SurvivorRatio=3 所以 survivor与eden的比例是1:3, 
Eden占年轻代的3/5是12m,s0,s1各占1/5是4m,从图中可以看到jvm内存分布符合设置的参数,方法区(非堆)30m. 
此时程序刚运行,dump了一下堆,可以看到刚生成的好多对象都是jmx相关的对象 
这里写图片描述

运行代码每次增加1m的对象到内存,当eden无法创建时会触发一次minor gc, 
这时由于survivor区无法承载eden中的全部对象,所以对象直接进入了老年代, 
见图: 
这里写图片描述
Eden中的对象一部分进入了s0,另一部分直接进入了年老代. 
这里写图片描述
上图,再次增加对象到eden,触发了一次minor gc,此时s1无法存放eden中的全部存活对象和s0中的存活对象, 老年代此时承担了内存担保的角色存放了多余的对象. 
这里写图片描述
上图中,由于程序中对象一直是存活的无法回收所以会一直进入老年代无法gc回收掉.

这里写图片描述
上图中老年代已满无法继续存放对象,此时full gc 有无法回收掉对象所以会报出

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">Exception <span class="hljs-operator" style="box-sizing: border-box;">in</span> thread <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"main"</span> java.lang.OutOfMemoryError: Java heap <span class="hljs-constant" style="box-sizing: border-box;">space</span>
堆内存溢出,程序退出.
看一下最后一次gc时堆中前后的数据对比:
{Heap <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">before</span> GC invocations=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span> (full <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>):
 PSYoungGen      total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16384</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11264</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fec00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000100000000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000100000000</span>)
  eden <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12288</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">91</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fec00000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ff7001d0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ff800000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ffc00000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ffc00000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000100000000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span>   <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ff800000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ff800000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ffc00000</span>)
 PSOldGen        total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40960</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40572</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fc400000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fec00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fec00000</span>)
  object <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40960</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">99</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fc400000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000feb9f1e0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fec00000</span>)
 PSPermGen       total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21248</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8605</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fa600000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fbac0000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fc400000</span>)
  object <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21248</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fa600000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae677a0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fbac0000</span>)
Heap <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">after</span> GC invocations=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span> (full <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>):
 PSYoungGen      total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16384</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11264</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fec00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000100000000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000100000000</span>)
  eden <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12288</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">91</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fec00000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ff7001d0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ff800000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ffc00000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ffc00000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000100000000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span>   <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ff800000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ff800000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000ffc00000</span>)
 PSOldGen        total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40960</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40538</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fc400000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fec00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fec00000</span>)
  object <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40960</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">98</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fc400000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000feb969f8</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fec00000</span>)
 PSPermGen       total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21248</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8537</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fa600000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fbac0000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fc400000</span>)
  object <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21248</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fa600000</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae56550</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fbac0000</span>)
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li></ul>

此次测试代码:

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">JConsoleTest</span> {</span>
    <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
     * 内存占位符对象 大约64k
     */</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> class ConObj{

        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] placeholder = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1024</span> * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1024</span>];
    }


    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">fillHeap</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> num) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throws</span> InterruptedException {
        List<ConObj> list = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ArrayList<ConObj>();
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;i < num;i++){
            Thread.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">500</span>);
            list.add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ConObj());
        }

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//由于方法未执行完毕 list尚在作用域中 此时 执行gc 会导致回收不掉</span>
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//System.gc();</span>

    }
    <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
     * 设置实例:
     *   1)vm args:
     *    -Xms1024m -Xmx1024m -XX:NewRatio=4 -XX:SurvivorRatio=4  -XX:MaxPermSize=16m
     *
     *   2)vm args:
     *    -Xms60m -Xmx60m -XX:NewRatio=2 -XX:SurvivorRatio=3  -XX:MaxPermSize=30m -XX:MaxTenuringThreshold=3 -XX:+PrintHeapAtGC
     *
     * 演示jconsole的内存页签  观察 eden survivor
     * vm args: -Xms100m -Xmx100m -XX:+UseSerialGC
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> args
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @throws</span> InterruptedException
     */</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>(String[] args) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throws</span> InterruptedException {
        Thread.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30000</span>);
        fillHeap(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//可以回收掉</span>
        System.gc();
        fillHeap(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
        fillHeap(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
        System.gc();
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">synchronized</span> (JConsoleTest.class){
            JConsoleTest.class.wait();
        }
    }
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li></ul>

配置举例2

<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">  -<span class="ruby" style="box-sizing: border-box;"><span class="hljs-constant" style="box-sizing: border-box;">Xmx60m</span> -<span class="hljs-constant" style="box-sizing: border-box;">Xms60m</span> -<span class="hljs-constant" style="box-sizing: border-box;">Xmn30m</span> -<span class="hljs-constant" style="box-sizing: border-box;">XX</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:+UseConcMarkSweepGC</span> -<span class="hljs-constant" style="box-sizing: border-box;">XX</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:+UseParNewGC</span>   -<span class="hljs-constant" style="box-sizing: border-box;">XX</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:MaxTenuringThreshold=</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>   -<span class="hljs-constant" style="box-sizing: border-box;">XX</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:+PrintGCApplicationStoppedTime</span> -<span class="hljs-constant" style="box-sizing: border-box;">XX</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:+PrintHeapAtGC</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

这里写图片描述

根据配置jvm堆大小为60m,其中分配给年轻代30m,年轻代中eden和s0,s1的比例默认是8:1, eden 24m,s0,s1各3m, 
这里写图片描述

上图第一次触发minor gc,同1老年代内存担保承担,很多对象没有经过survivor直接进入了老年代 
第一次minor gc的堆前后对比:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">{Heap <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">before</span> GC invocations=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> (full <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>):
 par <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">new</span> generation   total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">27648</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24576</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f7200000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>)
  eden <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24576</span>K, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f7200000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3072</span>K,   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8d00000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span>   <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3072</span>K,   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8d00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8d00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>)
 concurrent mark-sweep generation total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30720</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>)
 concurrent-mark-sweep perm gen total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21248</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8687</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fc2c0000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000100000000</span>)
Heap <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">after</span> GC invocations=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> (full <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>):
 par <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">new</span> generation   total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">27648</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3007</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f7200000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>)
  eden <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24576</span>K,   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f7200000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f7200000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3072</span>K,  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">97</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8d00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8fefd98</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span>   <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3072</span>K,   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8d00000</span>)
 concurrent mark-sweep generation total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30720</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8194</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>)
 concurrent-mark-sweep perm gen total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21248</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8687</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fc2c0000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000100000000</span>)
}   </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

第一次minor gc 应用暂停时间: 
Total time for which application threads were stopped: 0.0215440 seconds – 21.5ms 
这里写图片描述
上图是通过System.gc()触发了一次full gc之后的jvm内存分布. 
下面是full gc 前后heap前后对比:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">{Heap <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">before</span> GC invocations=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> (full <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>):
 par <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">new</span> generation   total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">27648</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17646</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f7200000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>)
  eden <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24576</span>K,  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">59</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f7200000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f804bb48</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3072</span>K,  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">97</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8d00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8fefd98</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span>   <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3072</span>K,   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8d00000</span>)
 concurrent mark-sweep generation total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30720</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8194</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>)
 concurrent-mark-sweep perm gen total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21248</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8703</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fc2c0000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000100000000</span>)
Heap <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">after</span> GC invocations=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> (full <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>):
 par <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">new</span> generation   total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">27648</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f7200000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>)
  eden <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24576</span>K,   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f7200000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f7200000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3072</span>K,   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8d00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8d00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>)
  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span>   <span class="hljs-constant" style="box-sizing: border-box;">space</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3072</span>K,   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% used [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8a00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f8d00000</span>)
 concurrent mark-sweep generation total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30720</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">923</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000f9000000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>)
 concurrent-mark-sweep perm gen total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21248</span>K, used <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8698</span>K [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fae00000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000fc2c0000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000100000000</span>)
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

应用暂停时间: 
Total time for which application threads were stopped: 0.0292275 seconds – 29ms

继续添加对象触发minor gc 
![这里写图片描述](https://img-blog.csdn.net/201504231118209{Heap before GC invocations=2 (full 1): 
par new generation total 27648K, used 24576K [0x00000000f7200000, 0x00000000f9000000, 0x00000000f9000000) 
eden space 24576K, 100% used [0x00000000f7200000, 0x00000000f8a00000, 0x00000000f8a00000) 
from space 3072K, 0% used [0x00000000f8d00000, 0x00000000f8d00000, 0x00000000f9000000) 
to space 3072K, 0% used [0x00000000f8a00000, 0x00000000f8a00000, 0x00000000f8d00000) 
concurrent mark-sweep generation total 30720K, used 923K [0x00000000f9000000, 0x00000000fae00000, 0x00000000fae00000) 
concurrent-mark-sweep perm gen total 21248K, used 8711K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000) 
Heap after GC invocations=3 (full 1): 
par new generation total 27648K, used 395K [0x00000000f7200000, 0x00000000f9000000, 0x00000000f9000000) 
eden space 24576K, 0% used [0x00000000f7200000, 0x00000000f7200000, 0x00000000f8a00000) 
from space 3072K, 12% used [0x00000000f8a00000, 0x00000000f8a62f30, 0x00000000f8d00000) 
to space 3072K, 0% used [0x00000000f8d00000, 0x00000000f8d00000, 0x00000000f9000000) 
concurrent mark-sweep generation total 30720K, used 923K [0x00000000f9000000, 0x00000000fae00000, 0x00000000fae00000) 
concurrent-mark-sweep perm gen total 21248K, used 8711K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000) 
}or gc 回收了eden中的大部分对象. 
程序暂停时间: 
Total time for which application threads were stopped: 0.0039866 seconds 
配置2代码:

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">JConsoleTest</span> {</span>
    <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
     * 内存占位符对象 大约64k
     */</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> class ConObj{

        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] placeholder = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1024</span> * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1024</span>];
    }
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">fillHeap</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> num) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throws</span> InterruptedException {
        List<ConObj> list = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ArrayList<ConObj>();
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;i < num;i++){
            Thread.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">500</span>);
            list.add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ConObj());
        }
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//由于方法未执行完毕 list尚在作用域中 此时 执行gc 会导致回收不掉</span>
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//System.gc();</span>

    }
    <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
     * 设置实例:
     *   1)vm args:
     *    -Xms1024m -Xmx1024m -XX:NewRatio=4 -XX:SurvivorRatio=4  -XX:MaxPermSize=16m
     *
     *   2)vm args:
     *    -Xms60m -Xmx60m -XX:NewRatio=2 -XX:SurvivorRatio=3  -XX:MaxPermSize=30m -XX:MaxTenuringThreshold=3 -XX:+PrintHeapAtGC
     *
     *   3) vm args:
     *      -Xmx60m -Xms60m -Xmn30m -XX:+UseConcMarkSweepGC -XX:+UseParNewGC   -XX:MaxTenuringThreshold=3   -XX:+PrintGCApplicationStoppedTime -XX:+PrintHeapAtGC
     * 演示jconsole的内存页签  观察 eden survivor
     * vm args: -Xms100m -Xmx100m -XX:+UseSerialGC
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> args
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @throws</span> InterruptedException
     */</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>(String[] args) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throws</span> InterruptedException {
        Thread.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15000</span>);
        fillHeap(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//可以回收掉</span>
        System.gc();
        fillHeap(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>);
        fillHeap(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>);
        fillHeap(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>);
        System.gc();
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">synchronized</span> (JConsoleTest.class){
            JConsoleTest.class.wait();
        }
    }
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li></ul>

上述是俩个较为简单的jvm参数配置演示.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值